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