CS 101 (Fall 2002)
Lecture 27
Subclassing

Virtual function calls work by calling the method that is the closest one defined for the actual run-time type of an object.

public class A {

  public A(int a, String s) {
  }

  public void meth1(int n) {
    Transcript.println("A meth1");
  }

  protected void meth2(boolean b) {
  }

  private void meth3(double d) {
  }

}

public class Aplus extends A {

  public Aplus(double d) {
     super(5, "hello");
  }

  public void meth1(int n) {
    // another meth1, better suited to Aplus
    // I can call meth2 from here because it is protected
    Transcript.println("Aplus meth1");
  }

  public void meth4() {
  }

}


  A obj = new Aplus(5.3);

  // what can be called on obj?   Only meth1

  obj.meth1(4);  // what prints out?  The Aplus version runs even though
                 // this is declared as an A-type object


  Aplus other = new Aplus(5.2);

  // what can be called on other?  meth1 and meth4