Polymorphism in Java

Polymorphism in Java is a concept of object-oriented programming that represents the possibility that the same method call behaves differently depending on the type of object over which the method is applied.

Polymorphism is derived from two Greek words: poly (more) and morphe (form). Thus, polymorphism means more forms.

Let’s look at this example:

class Vehicle {

  public void start() {
    System.out.println("The vehicle is started...");
  }
}

class Car extends Vehicle {

  public void start() {
    System.out.println("The car is started...");
  }
}

public class Test {

  public static void main(String[] args) {

    Vehicle vehicle = new Vehicle();

    Vehicle car = new Car();

    vehicle.start();
    car.start();
  }

}
Output: The vehicle is started… The car is started…
 
Here:

We can assign the Car object to the variable of the Vehicle type because Car is a subclass of the Vehicle.

So when we say – A car is a vehicle – we are not wrong.

See how the output differs.
This is because we first called the method using a variable that points to a Vehicle object, and the second call was via a variable that points to a Car object.

That’s polymorphism.

Depending on the object, Java will decide which version of the method to call in runtime.

Which method will be called depends on the type of object over which the method is called, not on the type of variable containing the object reference.

Note: Polymorphism is applied exclusively to methods, not on member data.

Some key points to remember:

  • A call to a method from a subclass is performed via a superclass variable.
  • The method that gets called must be declared in the superclass.
  • The method that gets called must be defined in the subclass.
  • The signature of methods in the superclass and subclass must be the same.
  • The return type of the method in a subclass must be the same as in the superclass, or at least to extend the return type from the superclass method.

One more example of Polymorphism:

class Vehicle {
    
  private String brand;
  
  public Vehicle (String brand) {
    this.brand = brand;
  }
    
  public void start() {
  }
}

class Car extends Vehicle {
    
  private String colour;
  private int numberOfSeets;
    
  public Car(String colour, int numberOfSeets) {
    super("Ford");
    this.colour = colour;
    this.numberOfSeets = numberOfSeets;
  }
    
  public void start(){
    System.out.println("The car is started...");
  }
}

class Truck extends Vehicle {
    
  private int length;
  
  public Truck (int length) {
    super("Opel");
    this.length = length;
  }
    
  public void start()  {
    System.out.println("The truck is started...");
  }
}

class Test {
    
  public static void main(String[] args) {
  
    //variable of class Vehicle points to the object of its subclass Car
    Vehicle car = new Car("Blue",5);
        
    //variable of class Vehicle points to the object of its subclass Truck
    Vehicle truck = new Truck(12);
        
    // call polymorphic methods start()
    System.out.println("Starting car: ");
        
    car.start();
    System.out.println("Starting truck: ");
    truck.start();
  }
}
Output: Starting car: The car is started… Starting truck: The truck is started…

As you can see from the example above, which method will be called depends on the type of object.

That’s it!

 

Leave a Reply

Your email address will not be published.