The process of representing one form in multiple forms is known as Polymorphism.

Polymorphism is derived from 2 greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms.

Polymorphism is not a programming concept but it is one of the principles of OOPs. For many objects oriented programming language, the polymorphism principle is common but whose implementations are varying from one objects oriented programming language to another object-oriented programming language.

Real life example of polymorphism in Java

Suppose if you are in class room that time you behave like a student, when you are in market at that time you behave like a customer, when you at your home at that time you behave like a son or daughter, Here one person present in different-different behaviors.

How to achieve Polymorphism in Java ?

In java programming the Polymorphism principal is implemented with method overriding concept of java.

Polymorphism principal is divided into two sub principal they are:

  • Static or Compile time polymorphism
  • Dynamic or Runtime polymorphism

Static polymorphism in Java is achieved by method overloading and Dynamic polymorphism in Java is achieved by method overriding


Note: Java programming does not support static polymorphism because of its limitations and java always supports dynamic polymorphism. 

Dynamic Binding

Dynamic binding always says create an object of base class but does not create the object of derived classes. The dynamic binding principle is always used for executing polymorphic applications.

The process of binding appropriate versions (overridden method) of derived classes that are inherited from a base class with a base class object is known as dynamic binding.

Advantages of dynamic binding along with polymorphism with method overriding are.

  • Less memory space
  • Less execution time
  • More performance                                                                                                                                                                                                                                                                                                                

Advantages of Dynamic Polymorphism
  • Dynamic Polymorphism allows Java to support overriding of methods which is central for run-time polymorphism.
  • It allows a class to specify methods that will be common to all of its derivatives while allowing subclasses to define the specific implementation of some or all of those methods.
  • It also allows subclasses to add specific methods subclasses to define the specific implementation of the same.                                                                                                                                

Static polymorphism

The process of binding the overloaded method within an object at compile time is known as Static polymorphism due to static polymorphism utilization of resources (main memory space) is poor because for each and every overloaded method a memory space is created at compile time when it binds with an object. In C++ environment the above problem can be solved by using dynamic polymorphism by implementing with virtual and pure virtual function so most of the C++ developer in real worlds follows only dynamic polymorphism.


Dynamic polymorphism

In the dynamic polymorphism method of the program binds with an object at runtime the advantage of dynamic polymorphism is allocating the memory space for the method (either for an overloaded method or for override method) at run time.


Conclusion

The advantage of dynamic polymorphism is effective utilization of the resources, So Java always uses dynamic polymorphism. Java does not support static polymorphism because of its limitation.


Example of Runtime Polymorphism in Java

In the below example we create two classes Person an Employee, Employee class extends Person class feature and override walk() method. We are calling the walk() method by the reference variable of Parent class. Since it refers to the subclass object and the subclass method overrides the Parent class method, the subclass method is invoked at runtime. Here method invocation is determined by the JVM not the compiler, So it is known as runtime polymorphism.


Program:

  class Person
{
void walk()
{
System.out.println("Can Run....");
}
}
class Employee extends Person
{
void walk()
{
System.out.println("Running Fast...");
}
public static void main(String arg[])
{
Person p=new Employee(); //upcasting
p.walk();
}
}

Output:

Running fast...

Do you Know?
Progamming is what actually means ?

What is software development ?

How we categorized software development in different manner.

C Programming
C Plus Plus
Python
Java Development
Web Designing
Javascript
Mysql
Oracle
We cover all the programming concepts in various programming languages, this tutorials are very help full for bigener as well as Experience developer, for the ease of understanding we categorized programming in different manner likewise.