Polymorphism in Java

Polymorphism means "many forms."

In simple words:

The same method call can behave differently depending on the object that is using it.

The main idea of polymorphism is that we can write common code, but different objects can provide their own behavior.


Simple Example

Imagine we have different animals:

Dog
Cat
Cow

All of them can make a sound.

But their sounds are different:

Dog → Bark
Cat → Meow
Cow → Moo

Instead of creating completely different method names like:

dogSound();
catSound();
cowSound();

we can use the same method:

sound();

and allow each object to decide what sound() should do.

That is polymorphism.


Example