GIGO: words unreadable aloud
Mishrogo Weedapeval
 

 

  Sunday 21 November 2004
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   comment/     



Click here to visit the Radio UserLand website. Click to see the XML version of this web page. © Copyright 2007 Doug Landauer .
Last update: 07/2/6; 12:42:39 .
Click here to send an email to the editor of this weblog.

November 2004
Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        
Oct   Dec

Previous/Next