Learn Java
import com.learnjava.oopfundamentals.Polymorphism;

Polymorphism in Java

@Concept

Polymorphism means a variable declared as a superclass type can refer to an object of any of its subclasses, and calling a method through that variable runs whichever version the object's actual, real class defines — not necessarily the version belonging to the variable's declared type. Animal a = new Dog(); is entirely legal, and a.makeSound() runs Dog's version of makeSound if Dog overrides it, even though a is declared as an Animal.

This is what makes it possible to write one method that accepts an Animal parameter and have it correctly handle a Dog, a Cat, or any other Animal subclass passed to it, each behaving according to its own actual type. The calling code doesn't need to know or check which specific subclass it received at all.

@HowItWorks
1

A superclass reference to a subclass object

Animal a = new Dog(); is legal, since a Dog genuinely is an Animal.

2

Which version actually runs

Calling a method through a depends on the object's real, actual class, not on how a happens to be declared.

3

Overridden methods are what makes this useful

If Dog overrides a method Animal declares, calling that method through an Animal-typed reference to a Dog still runs Dog's version.

4

One method, many behaviors

A method parameter typed as a superclass can accept any subclass, each responding to the same call in its own way.

5

The calling code stays generic

Code written against the superclass type never needs to check or care which specific subclass it actually received.

@Example
class Animal {
void makeSound() {
System.out.println("Some generic sound");
}
}
 
class Dog extends Animal {
void makeSound() {
System.out.println("Woof!");
}
}
 
public class PolymorphismDemo {
public static void main(String[] args) {
Animal a = new Dog();
a.makeSound();
}
}
Output
Woof!
1a is declared as an Animal, but the actual object it refers to is a Dog, created with new Dog().
2a.makeSound() looks at the object's real, actual class — Dog — not the declared type of the variable a.
3Since Dog overrides makeSound(), that overridden version runs instead of Animal's original one.
4The single println call inside Dog's version prints "Woof!", not Animal's generic message.
@UseCases
Writing one method that accepts a superclass type and correctly handles any of its subclasses
Storing objects of several related subclasses together in one collection typed as their shared superclass
Understanding why the object's real class, not the variable's declared type, decides which overridden method runs
Designing a class hierarchy specifically so callers can treat different subclasses uniformly

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.