// file: Version4.java // author: keller // purpose: demonstrating use of abstract methods // // description: // There is a base class and two derived classes. // Each overloads a method 'op'. // // In this version, virtual is not used at all. Consequently, the version // of op that gets called is determined strictly by the parameter type // (based or derived) through which the object is referred. abstract class Base { abstract String op(); }; class Derived_1 extends Base { String op() { return "op() of Derived_1"; } }; class Derived_2 extends Base { String op() { return "op() of Derived_2"; } }; class Version4 { static void testAsBase(Base object) { System.out.println("testAsBase: " + object.op()); } static void testAsDerived_1(Derived_1 object) { System.out.println("testAsDerived_1: " + object.op()); } static void testAsDerived_2(Derived_2 object) { System.out.println("testAsDerived_2: " + object.op()); } public static void main(String arg[]) { Derived_1 ob_1 = new Derived_1(); Derived_2 ob_2 = new Derived_2(); testAsBase(ob_1); testAsBase(ob_2); testAsDerived_1(ob_1); testAsDerived_2(ob_2); } } /* output of Version 1: testAsBase: op() of Base testAsBase: op() of Derived_1 testAsBase: op() of Derived_2 testAsDerived_1: op() of Derived_1 testAsDerived_2: op() of Derived_2 */