Polymorphism : Polymorphism « Class Definition « Java Tutorial






It means the ability of a single variable of a given type to be used to reference objects of different types and to automatically call the method that is specific to the type of object the variable references. (Ivor Horton's Beginning Java 2, JDK 5 Edition by Ivor Horton Wrox Press 2005 )

polymorphism works with derived class objects.

import java.util.Random;
class Animal {
  public Animal(String aType) {
    type = new String(aType);
  }
  public String toString() {
    return "This is a " + type;
  }
  public void sound(){
    System.out.println("Sound for an animal");
  }
  private String type;
}
class Dog extends Animal {
  public Dog(String aType){
   super(aType); 
  }
  public void sound() {
    System.out.println("Woof Woof");
  }
}
class Cat extends Animal {
  public Cat(String aName) {
    super("Cat"); 
    name = aName; 
    breed = "Unknown"; 
  }
  public Cat(String aName, String aBreed) {
    super("Cat"); 
    name = aName; 
    breed = aBreed; 
  }
  public String toString() {
    return super.toString() + "\nIt's " + name + " the " + breed;
  }
  public void sound() {
    System.out.println("Miiaooww");
  }
  private String name;
  private String breed;
}
class Duck extends Animal {
  public Duck(String aName) {
    super("Duck"); 
    name = aName; 
    breed = "Unknown";
  }
  public Duck(String aName, String aBreed) {
    super("Duck"); 
    name = aName; 
    breed = aBreed;
  }
  public String toString() {
    return super.toString() + "\nIt's " + name + " the " + breed;
  }
  public void sound() {
    System.out.println("Quack quackquack");
  }
  private String name;
  private String breed;
}
public class MainClass {
  public static void main(String[] args) {
    Animal[] theAnimals = { new Dog("A"), 
                            new Cat("C", "D"),
                            new Duck("E", "F") };
    Animal petChoice;
    Random select = new Random();
    for (int i = 0; i < 5; i++) {
      petChoice = theAnimals[select.nextInt(theAnimals.length)];
      System.out.println("\nYour choice:\n" + petChoice);
      petChoice.sound();
    }
  }
}
Your choice:
This is a A
Woof Woof
Your choice:
This is a Duck
It's E the F
Quack quackquack
Your choice:
This is a Duck
It's E the F
Quack quackquack
Your choice:
This is a Duck
It's E the F
Quack quackquack
Your choice:
This is a Duck
It's E the F
Quack quackquack








5.24.Polymorphism
5.24.1.Polymorphism
5.24.2.An example of polymorphism
5.24.3.Downcasting and Run-Time Type Identification (RTTI)
5.24.4.Constructors and polymorphism don't produce what you might expect
5.24.5.Dynamic Method Dispatch
5.24.6.Using run-time polymorphism.