What is polymorphism in programming?

Polymorphism is the capability of a method to do different things based on the object that it is acting upon. In other words, polymorphism allows you define one interface and have multiple implementations. I know it sounds confusing. Don’t worry we will discuss this in detail.

It is a feature that allows one interface to be used for a general class of actions.
An operation may exhibit different behavior in different instances.
The behavior depends on the types of data used in the operation.
It plays an important role in allowing objects having different internal structures to share the same external interface.
Polymorphism is extensively used in implementing inheritance.
Following concepts demonstrate different types of polymorphism in java.
1) Method Overloading
2) Method Overriding

Method Definition:
A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method’s name.

1)Method Overloading:
In Java, it is possible to define two or more methods of same name in a class, provided that there argument list or parameters are different. This concept is known as Method Overloading.

I have covered method overloading and Overriding below. To know more about polymorphism types refer my post Types of polymorphism in java: Static, Dynamic, Runtime and Compile time Polymorphism.

1) Method Overloading

To call an overloaded method in Java, it is must to use the type and/or number of arguments to determine which version of the overloaded method to actually call.
Overloaded methods may have different return types; the return type alone is insufficient to distinguish two versions of a method. .
When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call.
It allows the user to achieve compile time polymorphism.
An overloaded method can throw different exceptions.
It can have different access modifiers.

Rules for Method Overloading

Overloading can take place in the same class or in its sub-class.
Constructor in Java can be overloaded
Overloaded methods must have a different argument list.
Overloaded method should always be the part of the same class (can also take place in sub class), with same name but different parameters.
The parameters may differ in their type or number, or in both.
They may have the same or different return types.
It is also known as compile time polymorphism.

2) Method Overriding

Child class has the same method as of base class. In such cases child class overrides the parent class method without even touching the source code of the base class. This feature is known as method overriding.

Rules for Method Overriding:

applies only to inherited methods
object type (NOT reference variable type) determines which overridden method will be used at runtime
Overriding method can have different return type (refer this)
Overriding method must not have more restrictive access modifier
Abstract methods must be overridden
Static and final methods cannot be overridden
Constructors cannot be overridden
It is also known as Runtime polymorphism.

Dynamic Binding or Late Binding

When compiler is not able to resolve the call/binding at compile time, such binding is known as Dynamic or late Binding. Overriding is a perfect example of dynamic binding as in overriding both parent and child classes have same method. Thus while calling the overridden method, the compiler gets confused between parent and child class method(since both the methods have same name).

Leave a Reply

Your email address will not be published. Required fields are marked *