Polymorphism in Java

Polymorphism means many forms. When there is a hierarchy or relation between classes then it is called polymorphism in java.

Polymorphism in java can be achieved through inheritance. I mean making parent child relationships. If you are aware about inheritance concept in java, then it will be very easy to understand polymorphism.

Look at the below example of class hierarchies:


class Device {
  public void guessSize(){
    System.out.println("Device may have different sizes!");
  }
}

class Computer extends Device{
  public void guessSize(){
    System.out.println("The size of computer device usually big.");
  }
}

class Laptop extends Device{
  public void guessSize(){
    System.out.println("Laptop computer is smaller one.");
  }
}
class Mobile extends Device{
  public void guessSize(){
    System.out.println("You can carry a mobile phone in hand or pocket.");
  }
}

public class TestExample{ //must be file name
  public static void main (String[] args) {
    //create objects of device and subclasses 
    Device device = new Device();
    Device computer = new Computer();
    Device laptop = new Laptop();
    Device mobile = new Mobile();
    
    //execute the methods
    device.guessSize();
    computer.guessSize();
    laptop.guessSize();
    mobile.guessSize();
  }
}

/**
 * Output: 
 * 
 *Device may have different sizes!
 * The size of computer device usually
 * big
 * Laptop computer is smaller one.
 * You can carry a mobile phone in hand or
 *  pocket.
 */

You can see that how we reused the same method among three classes without defining method for each subclass. It's called method reusing.

Why polymorphism?

=> For code reusability. Define code once, and reuse it many times within related classes. By this mechanism, we can reuse attributes, methods among classes. But the precondition is inheritance.