DEV Community

Preethi Nandhagopal
Preethi Nandhagopal

Posted on

Polymorphism

Polymorphism:
An OOP's concept where a single action (method) can show different behaviors depending on the object that calls it.
Keypoint: one method, many forms.

Types of Polymorphism:
Method overloading:
Method overloading is a same method name and different type of argument or different number of argument in same class.
Also known as static polymorphism due to compile time polymorphism.
Achieved through method overloading.
Resolved by the compiler (at compile time).

Method overriding:
Method overriding is a same method name and same type of argument and same number of argument in different class.
Also known as dynamic polymorphism.
Resolved by the JVM at runtime.

Uses of polymorphism:
Code reusability-write one method, use it in many ways.
Flexibility-program can decide at runtime which method to call.
Extensibility-new classes can be added without changing old code.

Top comments (0)