next up previous contents
Next: Inheritance 3: Protected Up: Java Notes Previous: Polymorphism & Dynamic Binding

Inheritance 2: Method Overriding


/**
 *  This code is from the book:
 *    Winder, R and Roberts, G (1998)
 *    Developing Java Software
 *    John Wiley & Sons.
 *  It is copyright (c) 1997 Russel Winder
 *  and Graham Roberts.
 */

class Superclass{
  //May be overridden
  public void f(int x){
    System.out.println("Superclass f: " + x);
    //Always calls g declared by this class
    //as g is private
    g();
  }

  //Can't override this method
  private void g(){
    System.out.println("Superclass g");
  }

  //May be overridden
  protected void h(){
    System.out.println("Superclass h");
  }

  public void k(){
    System.out.println("Superclass k");
    //Always call superclass g as 
    //g cannot be overridden
    g();
    //Call h depending on type of object 
    //(type of this) using dynamic binding
    h();
    //Always call s in this class, as s is static
    s();
  }

  public static void s(){
    System.out.println("Superclass static s");
  }
}

class Subclass extends Superclass{
  //Overriding method - must be public
  public void f(int x){
    System.out.println("Subclass f: " + x);

    //Call g in this class
    g();
  }

  //New version of g 
  //not overriding version in Superclass
  private void g(){
    System.out.println("Subclass g");
  }

  //Overridding inherited h with increased access
  //Making this private or default access 
  //would be an error
  public void h(){
    System.out.println("Subclass h");
  }

  public static void s(){
    System.out.println("Subclass static s");
  }
}

public class l1Inherit2{//Override
  public static void main(String[] args){
    Superclass superclass = new Superclass();
    //Call superclass version of f
    superclass.f(1);
    superclass.h();
    superclass.k();
    superclass.s();

    Subclass subclass = new Subclass();
    //Call overridden subclass version of f and h
    subclass.f(2);
    subclass.h();

    //Call superclass k as it is not overridden
    subclass.k();
    //Call subclass s
    subclass.s();

    //Now set a variable of superclass type to 
    //reference a subclass object
    superclass = subclass;
    //Call *subclass* versions of f and h
    superclass.f(3);
    superclass.h();
    //Call superclass k for a subclass object
    superclass.k();
    //Call superclass s as it is static and 
    //not dynamically bound, so method called 
    //depends on type of reference.
    superclass.s();
  }
}
/******** sample compilation & run ********
# javac l1Inherit2.java 
# java l1Inherit2 
Superclass f: 1
Superclass g
Superclass h
Superclass k
Superclass g
Superclass h
Superclass static s
Superclass static s
Subclass f: 2
Subclass g
Subclass h
Superclass k
Superclass g
Subclass h
Superclass static s
Subclass static s
Subclass f: 3
Subclass g
Subclass h
Superclass k
Superclass g
Subclass h
Superclass static s
Superclass static s
# 
******************************************/



Ananda Amatya
9/15/1999