SE450: Review: OO Mechanisms: Subtyping (Polymorphism) [12/25] Previous pageContentsNext page

Objects may be seen at more abstract (or more concrete) types.

The set of messages available to the user depend upon the declared type at which the user sees the object.

interface I { public void m(); }
class D implements I { public void m() { System.out.println("D.m");
                       public void f() { System.out.println("D.f"); }
class Main {
  public static void main(String[] args) {
    I x = new D();
    x.f();  // access denied -- won't compile
    x.m();  // prints "D.m"
  }
}

Previous pageContentsNext page