Overview
Polymorphism is to use common interface instead of concrete implementation while coding.
The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
Quick Summary:
A subclass instance can be assigned to a superclass’ reference.
Once substituted, we can invoke methods defined in super-class; we CANNOT invoke methods from sub-class.
if sub-class overrides inherited methods from the super-class, the overridden versions will be invoked.
Reference cannot point to superclass.
Advantages
Simpler programs
eg.
superRef[] = new Circle[2];
superRef[0] = new Cylinder();
superRef[1] = new Circle();Just 1 single array to represent different objects
Better re-usability
A practical example
Cylinder is a subclass of Circle. We gonna do this:
Circle c1 = new Cylinder();
The code is below:
public class PolymorphismSubstitutabilityDemo {
public static void main(String[] args) {
Circle c1 = new Cylinder(1, "white", 10);
System.out.println(c1.getClass());
System.out.println(c1.getRadius());
System.out.println(c1.getArea());
}
}
class Circle {
int radius;
String color;
public Circle(int a, String b) {
this.radius = a;
this.color = b;
}
public int getRadius() {
return radius;
}
public double getArea() {
return 3.14159 * Math.pow(radius, 2);
}
}
class Cylinder extends Circle {
int height;
public Cylinder(int a, String b, int c) {
super(a, b);
this.height = c;
}
public double getArea() {
return super.getArea() * height;
}
}
The output of execution:
class Cylinder
1
31.4159