Polymorphism
The ability of objects to take on many forms. A single interface can represent different underlying types, with behavior determined at runtime or compile time.
1What is Polymorphism?
A universal remote has a "Power" button. When you press it, different devices respond differently - TV turns on, AC starts cooling, speaker plays music.Same action, different behaviors based on the device.
You call draw() on a Shape. If it is a Circle, it draws a circle. If it is a Rectangle, it draws a rectangle. The calling code does not know or care which shape it is - it just calls draw().
Polymorphism in Action
Shape shape = ???shape.draw()2Types of Polymorphism
add(int a, int b)
add(double a, double b)
add(String a, String b)Animal a = new Dog();
a.speak(); // Woof!
a = new Cat();
a.speak(); // Meow!Overriding: Same method signature, different implementation, parent-child classes.
3Method Overloading (Compile-Time)
Multiple methods with the same name but different parameter lists. The compiler decides which method to call based on arguments.
public class Calculator {
// Overloaded methods - same name, different parameters
// Version 1: Two integers
public int add(int a, int b) {
System.out.println("Adding two integers");
return a + b;
}
// Version 2: Three integers
public int add(int a, int b, int c) {
System.out.println("Adding three integers");
return a + b + c;
}
// Version 3: Two doubles
public double add(double a, double b) {
System.out.println("Adding two doubles");
return a + b;
}
// Version 4: String concatenation
public String add(String a, String b) {
System.out.println("Concatenating strings");
return a + b;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
// Compiler picks the right method based on arguments
System.out.println(calc.add(5, 10)); // Version 1
System.out.println(calc.add(1, 2, 3)); // Version 2
System.out.println(calc.add(2.5, 3.5)); // Version 3
System.out.println(calc.add("Hello", "World")); // Version 4
}
}4Method Overriding (Runtime)
Child class provides its own implementation of a method defined in the parent class. The method called depends on the actual object type, not the reference type.
// Parent class
abstract class Shape {
protected String color;
public Shape(String color) {
this.color = color;
}
// Method to be overridden
public abstract double calculateArea();
public void display() {
System.out.println("This is a " + color + " shape");
}
}
// Child 1
class Circle extends Shape {
private double radius;
public Circle(String color, double radius) {
super(color);
this.radius = radius;
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
@Override
public void display() {
System.out.println("This is a " + color + " circle with radius " + radius);
}
}
// Child 2
class Rectangle extends Shape {
private double width, height;
public Rectangle(String color, double width, double height) {
super(color);
this.width = width;
this.height = height;
}
@Override
public double calculateArea() {
return width * height;
}
}
// Usage - Polymorphism in action!
public class Main {
public static void main(String[] args) {
// Array of Shape references, holding different objects
Shape[] shapes = {
new Circle("red", 5),
new Rectangle("blue", 4, 6),
new Circle("green", 3)
};
// Same code works for all shapes!
for (Shape shape : shapes) {
shape.display();
System.out.println("Area: " + shape.calculateArea());
System.out.println();
}
// Total area calculation without knowing specific types
double totalArea = 0;
for (Shape shape : shapes) {
totalArea += shape.calculateArea();
}
System.out.println("Total area: " + totalArea);
}
}This is a red circle with radius 5.0 Area: 78.54 This is a blue shape Area: 24.0 This is a green circle with radius 3.0 Area: 28.27 Total area: 130.81
5Why Polymorphism Matters
Shape shape = new Circle("red", 5);- shape reference holds Circle objectshape.calculateArea();- compiler sees Shape reference- At runtime, JVM looks at actual object type (Circle)
- JVM finds Circle's calculateArea() method in vtable
- Circle's method executes, returns 78.54