Polymorphism

Polymorphism#

Polymorphism means “many shapes” in Greek. So we already know the power of inheritance and happily use it. But there comes this problem. Say we have a parent class and a few child classes which inherit from it. Sometimes we want to use a collection — for example a list — which contains a mix of all these classes. Or we have a method implemented for the parent class — but we’d like to use it for the children, too.

This can be solved by using polymorphism.

Simply put, polymorphism gives a way to use a class exactly like its parent so there’s no confusion with mixing types. But each child class keeps its own methods as they are.

Benefits of Polymorphism:#

  • Objects of different types can be passed through the same interface

  • Method overriding

  • Method overloading