/** * This code is from the book: * Winder, R and Roberts, G (1998) * Developing Java Software</em> * John Wiley & Sons. * It is copyright (c) 1997 Russel Winder * and Graham Roberts. */ abstract class Superclass{ //Non-abstract classes must override and //implement this method. public abstract void f(); //Standard method - can be shared or overriden public void h(){ System.out.println("Superclass:h"); } //Can't declare static methods as abstract //public abstract static void x(); //Error } class Subclass extends Superclass{ //Overridden inherited abstract method //This class can have instances public void f(){ System.out.println("Subclass"); } } //This class does not override the inherited //abstract method f and so must be abstract abstract class Subclass2 extends Superclass{ //Declare a new abstract method public abstract void g(); } //This class implements both inherited abstract //methods and can have instance objects class Subclass3 extends Subclass2{ //Must override inherited abstract method f here public void f(){ System.out.println("Subclass2:f"); } //Must also override inherited //abstract method g here public void g(){ System.out.println("Subclass2:g"); } //Overridden inherited method //Allowed but not necessary public void h(){ System.out.println("Superclass:h"); } } class l1Inherit8{//Abstract2 public static void main(String[] args){ //Can't create Superclass objects //Superclass superclass = new Superclass(); //Error abstract //Can't create Subclass2 objects //Subclass subclass2 = new Subclass2(); //Error abstract Subclass subclass = new Subclass(); subclass.f(); subclass.h(); Subclass3 subclass3 = new Subclass3(); subclass3.f(); subclass3.g(); subclass3.h(); } } /******** sample compilation & run ******* # javac l1Inherit8.java # java l1Inherit8 Subclass Superclass:h Subclass2:f Subclass2:g Superclass:h # ******************************************/