Polymorphism in Object Oriented Programming

Polymorphism means poly multiple forms. Many shapes. In object oriented programming, polymorphism means the type of object is determined as late as at runtime. What does it mean, and what do we achieve by this? Let us take an example. We have following classes.

Car

Truck

Bus

We can drive all of them, hence there is a drive method in all classes. But till run time we don’t know whether we are going to drive a car, or a truck or a bus. Now the program also needs to provide this feature. It needs to allow selection of actual vehicle at very last moment, which is run time. Compiler of program should not object on this.

To achieve this, we add two more entities to above classes. First is an interface which has only one method called drive() without any impelmentation, and second is an abstract class called AbstractVehicle which has everything common amongst above three classes.

Interface – VehicleInterface (drive() method)

|

implements

|

Abstract Class – AbstractVehicle (drive() method)

|

Concrete Classes – Car, Truck, and Bus. (implemented drive() method by each)

Here actual implementation of drive method is available in our three concrete classes Car, Truck and Bus. Now we come to polymorphic usage of these classes. The program which is actually going to use these classes does not know whether it is going to be a car, or a truck or a bus. Hence it can not call drive() method directly on any of these identified classes. Then what is solution? Let the program call drive() method on interface only, or on the abstract class, leaving actual concrete class determination for last moment.

Let us take this example in Java language and see how the code works.

VehicleInterface.java

package polymorphism;

public interface VehicleInterface {

	public void drive();
}
AbstractVehicle.java
package polymorphism;

public abstract class AbstractVehicle implements VehicleInterface{
	private String engine;
	private String color;
	private int wheels;

	public String getEngine() {
		return engine;
	}

	public void setEngine(String aengine) {
		this.engine = aengine;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String acolor) {
		this.color = acolor;
	}

	public int getWheels() {
		return wheels;
	}

	public void setWheels(int awheels) {
		this.wheels = awheels;
	}
}
Car.java
package polymorphism;

public class Car extends AbstractVehicle {

	@Override
	public void drive() {
		System.out.println("Driving a Car.");
	}

}
Truck.java
package polymorphism;

public class Truck extends AbstractVehicle {

	@Override
	public void drive() {
		System.out.println("Driving a Truck.");
	}

}
Bus.java
package polymorphism;

public class Bus extends AbstractVehicle {

	@Override
	public void drive() {
		System.out.println("Driving a Bus.");

	}

}
UsingPolymorphism.java
package polymorphism;

import java.util.List;
import java.util.ArrayList;

public class UsingPolymorphism {

	public static void main(String[] args) {
		UsingPolymorphism upmorph= new UsingPolymorphism();
		List vehicles = new ArrayList();
		vehicles.add(new Car());
		vehicles.add(new Truck());
		vehicles.add(new Bus());

		upmorph.driveVehicles(vehicles);
	}

	private void driveVehicles(List vehicles){
		for( VehicleInterface vehicle:vehicles){
			vehicle.drive();
		}
	}
}
In Java if a class is not defined final, then there is always polymorphism in working.
Extensibility:
Polymorphism makes classes (group of classes) extensible. If we consider above example, then we can add one more vehicle and the program UsingPolymorphism will still be able to drive() the vehicle. This design reduces future modification cost of applications.

Disadvantages of Polymorphism:

Polymorphism reduces readability of the program. One needs to visualize runtime behaviour of program to identify actual execution time class involved. It also becomes difficult to navigate through classes implementing this concept. Even sofasticated IDEs can not provide this navigation feature. This adds to the maintainance cost to some extent.

2 Comments

  1. sir,
    when i am compiling the program, i am getting error.
    i pasted below error…

    C:Program FilesJavajdk1.6.0bin>javac UsingPolymorphism.java
    UsingPolymorphism.java:21: incompatible types
    found : java.lang.Object
    required: VehicleInterface
    for( VehicleInterface vehicle:vehicles)
    ^
    Note: UsingPolymorphism.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    1 error

Leave a Reply

Your email address will not be published.


*