Module 6: Inheritance and Polymorphism
Polymorphism in Action
Use one parent type to work with many child objects while Java chooses the correct overridden method at runtime.
Author
Java Learner Editorial Team
Reviewer
Technical review by Java Learner
Last reviewed
2026-04-16
Java version
Java 25 LTS
Learning goals
- Understand the idea of one type reference pointing to different child objects
- See runtime method selection in practice
- Write more flexible code with parent references
Polymorphism means one reference type can work with many related object types: A variable of type Animal can point to a Dog or a Cat.
Java chooses the correct overridden method at runtime: Even if the variable type is the parent class, the actual object decides which method runs.
Why this matters: Code can stay general while still getting specific behavior from each child object.
Beginner benefit: You can process many related objects in one loop without writing a separate method call for every child class.
Runnable examples
One parent type, different child behavior
class Animal {
void makeSound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Woof");
}
}
class Cat extends Animal {
@Override
void makeSound() {
System.out.println("Meow");
}
}
public class Main {
public static void main(String[] args) {
Animal first = new Dog();
Animal second = new Cat();
first.makeSound();
second.makeSound();
}
}Expected output
Woof Meow
Mini exercise
Create an array of `Animal` references containing different child objects and call `makeSound()` on each one.
Summary
- Polymorphism lets one parent type work with many child objects.
- The actual object decides which overridden method runs.
- This makes code more flexible and reusable.
Next step
Next, use abstract classes when a parent should define a template but should not be created directly.
Sources used