Polymorphism
in Java
Learn how one reference type can point to many object forms: the core principle that makes object-oriented code flexible and extensible.
What Is Polymorphism?
Polymorphism comes from Greek: poly (many) + morph (form). In Java, it means a single variable of a superclass type can refer to objects of different subclass types, and the correct method is called based on the actual object, not the declared type.
Why Polymorphism Matters
The Inheritance Foundation
The Core Rule
// Superclass reference, subclass object Animal myPet = new Dog(); myPet.speak(); // prints "Woof!"
Compile-Time vs Run-Time Type
Overloading vs Overriding
| Overloading | Overriding | |
|---|---|---|
| Where | Same class | Subclass re-defines superclass method |
| Signature | Same name, different parameters | Same name, same parameters |
| Resolved at | Compile time | Run time |
| Polymorphism type | Static (compile-time) | Dynamic (run-time) |
| Annotation | None required | @Override recommended |
How to Apply Polymorphism
Example 1: Shape Area
class Shape { public double area() { return 0; } } class Circle extends Shape { double radius; public Circle(double r) { radius = r; } @Override public double area() { return Math.PI * radius * radius; } } class Rectangle extends Shape { double w, h; public Rectangle(double w, double h) { this.w=w; this.h=h; } @Override public double area() { return w * h; } }
Using the Shape Reference
public static void printArea(Shape s) { System.out.println("Area: " + s.area()); } // In main: Shape c = new Circle(5); Shape r = new Rectangle(4, 6); printArea(c); // Area: 78.54... printArea(r); // Area: 24.0
Example 2: Polymorphic Arrays
Animal[] pets = new Animal[3]; pets[0] = new Dog(); pets[1] = new Cat(); pets[2] = new Bird(); for (Animal a : pets) { a.speak(); // each calls its own version }
Example 3: Method Parameters
class Vehicle { public String fuelType() { return "Unknown"; } } class ElectricCar extends Vehicle { @Override public String fuelType() { return "Electric"; } } class GasTruck extends Vehicle { @Override public String fuelType() { return "Gasoline"; } } public static void refuel(Vehicle v) { System.out.println("Fuel: " + v.fuelType()); } refuel(new ElectricCar()); // Fuel: Electric refuel(new GasTruck()); // Fuel: Gasoline
What You Cannot Do
Animal a = new Dog(); a.speak(); // ✓ Animal has speak() a.fetch(); // ✕ COMPILE ERROR: Animal has no fetch()
How the JVM Decides
Common Mistakes
Polymorphism Summary
| Concept | Details |
|---|---|
| Definition | One superclass reference, many subclass behaviors |
| Requirement | Inheritance + method overriding |
| Compile time | Checks declared (reference) type for method existence |
| Run time | Dispatches to actual (object) type's override |
| Limitation | Cannot call subclass-only methods via superclass ref |
| Key benefit | Write code once, works for all subclasses |
Question 1 of 5
Given:
Animal a = new Cat();
a.speak();
Which version of speak() is executed?
Question 2 of 5
If Dog has a method fetch() that Animal does not, what happens when you write:
Animal a = new Dog(); a.fetch();
Question 3 of 5
A method is defined as:
public void feed(Animal a) { ... }
Which of the following is a valid call?
Question 4 of 5
What is the key difference between method overloading and method overriding?
Question 5 of 5
Consider this code:
Shape[] shapes = { new Circle(3), new Rectangle(2, 5), new Circle(1) };
for (Shape s : shapes) {
System.out.println(s.area());
}
Circle.area() returns \( \pi r^2 \). Rectangle.area() returns \( w \times h \).
What does this loop print (approximately)?