Class extension mechanism allows you to model in stages.

class Creature {
  void talk() {
    System.out.println(""); 
  } 
}

class Cow extends Creature {
  void talk() {
    System.out.println("Mooo.");
  }
}

class Dolphin extends Creature {
  void talk() {
    System.out.println("Squeak.");
  }
}

class Fish extends Creature {

}

class Bass extends Fish {

}

class Example {
  public static void main(String[] args) {
    Cow c = new Cow(); 
    Creature a = new Cow(); // polymorphism
    Creature b = c; // polymorphism 
    Creature d = new Bass(); // also polymorphism 
    Fish f = (Bass)d; // without casting won't work 
    Fish g = new Bass(); // easy to see why
  }
}

Polymorphism in real life

  Luis Scola plays for the Pacers

  Manu Ginobili plays for the Spurs

  They are enemies in NBA. They are incompatible, like Cow and Dolphin. 

  They are not on the same team in NBA.  

  However they play on the same olympic team: Argentina. 

http://en.wikipedia.org/wiki/Basketball_at_the_2004_Summer_Olympics


class Creature {
  void talk() {
    System.out.println(""); 
  } 
}


class Cow extends Creature {
  void talk() {
    System.out.println("Moo.");
  }
}

class Dolphin extends Creature {
  void talk() {
    System.out.println("Squeak.");
  }
}

class Fish extends Creature {

}

class Bass extends Fish {

}

class Example {
  public static void main(String[] args) {
    Cow c = new Cow(); 
    Creature a = new Cow(); // polymorphism
    Creature b = c; // polymorphism 
    Creature d = new Bass(); // also polymorphism 
    Fish f = (Bass)d; // without casting won't work 
    Fish g = new Bass(); // easy to see why
    
    c.talk(); // expect: Moo.
    a.talk(); // expect: Moo.
    Creature h = new Dolphin(); 
    h.talk(); // expect: Squeak.
    
    Creature i = new Creature(); // not good
    i.talk(); // quiet is the only decent alternative 
    
    Fish j = new Fish(); 
    j.talk(); 

    Fish k = new Bass(); 
    k.talk(); 
  }
}


---

abstract class Creature {
  abstract void talk();
}

class Cow extends Creature {
  void talk() {
    System.out.println("Moo.");
  }
}

class Dolphin extends Creature {
  void talk() {
    System.out.println("Squeak.");
  }
}

abstract class Fish extends Creature {

}

class Bass extends Fish {
  void talk() {
    System.out.println("Snort.");  
  }
}

class Example {
  public static void main(String[] args) {
    Cow c = new Cow(); 
    Creature a = new Cow(); // polymorphism
    Creature b = c; // polymorphism 
    Creature d = new Bass(); // also polymorphism 
    Fish f = (Bass)d; // without casting won't work 
    Fish g = new Bass(); // easy to see why
    
    c.talk(); // expect: Moo.
    a.talk(); // expect: Moo.
    Creature h = new Dolphin(); 
    h.talk(); // expect: Squeak.
    
    // Creature i = new Creature(); // abstract now 
    // i.talk(); // can't instantiate abstract classes  
    
    // Fish j = new Fish(); // can't instantiate abstract classes 
    // j.talk(); 

    Fish k = new Bass(); // polymorphism
    k.talk(); // expect: Snort. (dynamic method lookup) 
  }
}

---

interface Creature {
  void talk();
}

class Cow implements Creature {
  public void talk() {
    System.out.println("Moo.");
  }
}

class Dolphin implements Creature {
  public void talk() {
    System.out.println("Squeak.");
  }
}

interface Fish extends Creature {
  void sing(); 
}

class Bass implements Fish {
  public void talk() {
    System.out.println("Snort.");  
  }
  public void sing() {
    System.out.println("Cluck.");  
  }
}


class Example {
  public static void main(String[] args) {
    Creature c = new Cow(); // Moo.
    c.talk(); 
    Creature a = new Bass(); // Snort.
    a.talk(); 
    // a.sing(); // not a Creature property
    ((Fish)a).sing(); // Cluck.
  }
}

--