PolymorphismΒΆ

Polymorphism is a big word that you can break down into “poly” which means many and “morphism” which means form. So, it just means many forms. In Java it means that the method that gets called at run-time (when the code is run) depends on the type of the object at run-time. This is simliar to a toddler toy that has pictures of animals and when a handle is pulled an arrow spins. When the arrow stops the toy plays the sound associated with that animal.

Picture of a See N Say toy

If you were simulating this toy in software you could create an Animal class that had an abstract makeNoise method. Each subclass of Animal would override the makeNoise method to make the correct noise for that type.

In Java an object variable has both a declared type and a run-time type or actual type. The declared type of a variable is the type that is used in the declaration. The run-time type or actual type is the class that actually creates the object. The nameList variable declared below has a declared type of List and an actual or run-time type of ArrayList.

List<String> nameList = new ArrayList<String>();

Any object variable can refer to an object of the declared type or any descendant (subclass) of the declared type at run-time. At compile time the compiler uses the declared type to check that the methods you are trying to use are available to an object of that type. The code won’t compile if the methods don’t exist in that class or some parent class of that class. At run-time the actual method that is called depends on the actual type of the object. Remember that an object keeps a reference to the class that created it (an object of the class called Class). When a method is called at run-time the first place that is checked for that method is the class that created the object. If the method is found there it will be executed. If not, the parent of that class will be checked and so on until the method is found.

Check your understanding

10-9-1: What is the output from running the main method in the Shape class?

public class Shape {
   public void what() { System.out.print("Shape ");}

   public static void main(String[] args)

      Shape[] shapes = {new Shape(), new Rectangle(), new Square(),
                        new Circle()};
      for (Shape s : shapes)
      {
         System.out.print(s.what() + " ");
      }

}

public class Rectangle extends Shape {
   public void what() { System.out.print("Rectangle "); }
}

public class Square extends Rectangle {
}

public class Oval extends Shape {
   public void what() { System.out.print("Oval "); }
}

public class Circle extends Oval {
   public void what() { System.out.print("Circle ");}
}






Check your understanding

10-9-2: What is the output from running the main method in the GradStudent class?

public class Student {
   public String getFood() {
      return "Pizza";
   }
   public String getInfo()  {
     return this.getFood();
   }
}

public class GradStudent extends Student {
  public String getFood() {
     return "Taco";
  }

  public static void main(String[] args)
  {
     Student s1 = new GradStudent();
     s1.getInfo();
  }
}





10-9-3: What is the output from running the main method in the RaceCar class?

public class Car
{
  private int fuel;

  public Car() { fuel = 0; }
  public Car(int g) { fuel = g; }

  public void addFuel() { fuel++; }
  public void display() { System.out.print(fuel + " "); }
}

public class RaceCar extends Car
{
  public RaceCar(int g) { super(2*g); }

  public static void main(String[] args)
  {
     Car car = new Car(5);
     Car fastCar = new RaceCar(5);
     car.display()
     car.addFuel();
     car.display();
     fastCar.display();
     fastCar.addFuel();
     fastCar.display();
  }
}





10-9-4: Given the following class definitions and a declaration of Book b = new Dictionary which of the following will cause a compile-time error?

public class Book
{
   public String getISBN()
   {
      // implementation not shown
   }

   // constructors, fields, and other methods not shown
}

public class Dictionary extends Book
{
   public String getDefinition(String word)
   {
      // implementation not shown
   }
}




10-9-5: Assume that the following declaration appears in a client program Base b = new Derived();. What is the result of the call b.methodOne()?

public class Base
{
   public void methodOne()
   {
      System.out.print("A");
      methodTwo();
   }

   public void methodTwo()
   {
      System.out.print("B");
   }
}

public class Derived extends Base
{
   public void methodOne()
   {
      super.methodOne();
      System.out.print("C");
   }

   public void methodTwo()
   {
      super.methodTwo();
      System.out.print("D");
   }
}