Polymorphism
Polymorphism, in Greek means “many forms”. It simply refers
to the different behavior of same thing in different ways according to the
situations based on type of inputs. It simply works on the fundamental of
message passing.
Consider the real time example of a Fund transfer:
Consider fund transfer as the common functionality used. Now
based on the option selected for payment type the payment will be done i.e. the
type of transaction as RTGS or NEFT can be selected by us and the transfer will
take place based on that input. Thus you can see how same functionality behaved
differently based on the types of mode.
Secondly, consider our sight as one function. If we see a
lion coming towards us, we will run while if it’s a rabbit, we will go towards
it. Here seeing is a function which is behaving in different manner based on
the type of animal we are seeing.
Polymorphism in JAVA is
one of the OOP concepts along with inheritance, encapsulation and abstraction.
It helps in making the code more flexible and reusable for future enhancements.
It is achieved in two ways:
Static Polymorphism (method overloading)
Dynamic Polymorphism (method overriding)
1. Static
Polymorphism :
This is termed as overloading /compile time binding. Here the compiler
decides which method would be called based on the type/number of parameters
getting passed while calling the method. The binding happens at compile time.
Overloaded methods are methods which have different signature. Method
signature includes name, number of arguments, and type of arguments and order
of arguments to that method.
So the overloaded methods
a.
Have the same name.
b.
Have different parameter list.(number of parameters/type
of parameters/order of parameters)
c.
May appear in same class or subclass.
For example: see the below code snippets to understand different ways for
overloading a method.
 |
types of overloading methods |
Constructor overloading is one of the application of static polymorphism
in which a class can have multiple constructor with same name varying in list
of arguments getting passed to it. It is as shown in below code.
 |
Constructor Overriding |
2. Dynamic
Polymorphism:
This is termed as overriding or run-time binding. In this type of
polymorphism, the calling method is decided at run time based on the type of
object calling the method. The overridden methods have the same name and same
signature. Overriding helps in achieving
flexibility and extensibility as the new functionality can be introduced with
minimal changes in the code.
Overriding methods:
a. Can not appear in same class. They need to be
defined in sub class.
b. The accessibility of the overridden method cannot
be reduced i.e. for example public method cannot be made private/default/protected
in sub class.
c. The private or static or final methods cannot be
overridden.
d. Signature and name must be exactly same.
e. Overriding method cannot throw a checked exception
which is higher in hierarchy than what is thrown by the overridden method. For
example: If overridden method has thrown an IOException then the overriding
method cannot throw java.lang.Exception.
This will be clear with the below code:
The base class is Shape which has one method defined as Area() which will be overridden in the sub classes Circle and Rectangle.
 |
Base Class |
 |
Circle sub class extending Shape class. Area() method is oerridden |
 |
Rectangle sub class extending Shape class and overriding Area() method. |
 |
Main class calling different methods with different objects |
The result for above class execution is as below. It is clear that the methods are getting called based on the type of object which is getting created.