July 7, 2008

at Monday, July 07, 2008 Posted by Billy

Chapter: Interfaces

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 Java:
package chapter.interfaces;

abstract class Base {
}

class Sub extends Base {
public void foo() {
System.out.println("Sub foo");
}
}

public class Exercise4 {
private static void bar(Base b) {
((Sub) b).foo();
}

public static void main(String[] args) {
bar(new Sub());
}
}

0 comments: