What is Polymorphism principle Explain different forms of Polymorphism
Polymorphism : It refers to a principle in which an object can have many different forms or stages. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class and used in different forms called Polymorphism.
Different forms of Polymorphism : In java we have two types of Polymorphism:
- Dynamic or Run time polymorphism : This kind of polymorphism existed at run-time. Java compiler does not understand which method to called at compile. Only JVM decides which method is to be called at run-time.
Method overriding is example of Dynamic or Run time polymorphism because method binding between method call and method definition happens at run time and it’s depend on the object of the class (when object create at run time and goes to heap) .
- Static or Compile time polymorphism : This kind of polymorphism exhibited at compile time. Java compiler knows in advance which method to be called. Method overloading and method overriding using static methods; method overriding using private or final methods are examples for static polymorphism
Method overloading is an example of Static or Compile time polymorphism because method binding between method call and method definition happens at compile time and it’s depend on the reference of the class(when objects create at compile time and goes to stack).