Chapter 7, Exercise 11
Exercise 11: Create a base class with two methods.
In the first method, call the second method.
Inherit a class and override the second method.
Create an object of the derived class, upcast it to the base type,
and call the first method. Explain what happens.
Clean enough:
package x;
class bc2m {
def fmeth ( ) : Int = smeth();
def smeth ( ) : Int = 12;
}
class c7xFch extends bc2m {
override def smeth () : Int = 22;
}
object c7xF {
def main ( args : Array[String] ) = {
val derobj = new c7xFch;
val basup : bc2m = derobj.asInstanceOf[bc2m];
W.rn( "der upcast base fmeth => " + basup.fmeth() );
}
}
It outputs der upcast base fmeth => 22
.
Because the call to smeth in fmeth is dispatched at run time.
11:43:06 PM