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();
}
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;
}
}
package polymorphism;
public class Car extends AbstractVehicle {
@Override
public void drive() {
System.out.println("Driving a Car.");
}
}
package polymorphism;
public class Truck extends AbstractVehicle {
@Override
public void drive() {
System.out.println("Driving a Truck.");
}
}
package polymorphism;
public class Bus extends AbstractVehicle {
@Override
public void drive() {
System.out.println("Driving a Bus.");
}
}
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();
}
}
}









sir,
when i am compiling the program, i am getting error.
i pasted below error…
C:\Program Files\Java\jdk1.6.0\bin>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
Give Advantages of Polymorphous
Leave your response!
Subscribe
Subscribe Via Email
Recent Posts
Recent Comments
Tags
Categories
Archives