GIGO: words unreadable aloud
Mishrogo Weedapeval
 

 

  Tuesday 30 November 2004
Chapter 7, Exercise 14

Create an abstract class with no methods. Derive a class and add a method. Create a static method that takes a reference to the base class, downcasts it to the derived class, and calls the method. In main(), demonstrate that it works...

package x; 
abstract class Chaos     {                             }
class Blur extends Chaos { def focus = W.rn( "ford" ); }
object c7xI {
    def castor ( cha : Chaos ) = {
        val lens = cha.asInstanceOf[ Blur ];
        lens focus;
    }
    def main ( args: Array[String] ) = {
        val c = new Blur;
        castor( c );
    }
}

... Now put the abstract declaration for the method in the base class, thus eliminating the need for the downcast.

package x; 
abstract class Chaos     { def focus : Unit;           }
class Blur extends Chaos { def focus = W.rn( "ford" ); }
object c7xI_2 {
    def castor ( cha : Chaos ) = {
        cha focus;
    }
    def main ( args: Array[String] ) = {
        val c = new Blur;
        castor( c );
    }
}

11:58:38 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:44 .
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