Polymorphism in Java

Poly  means  “Many”
Morph  means  “Form”
Therefore Polymorphism refers to “Many Forms”

Polymorphism is the ability of an object to take on many forms.

 It allows to process objects differently depending on their data type or class.It 
Commonly polymorphism occurs when a parent class reference is used to refer to a child class object.It gives the method  , capability to do different things based on the object that it is acting upon. Polymorphism enables us to define one interface and have multiple implementations.

A Java object that passes more than one IS-A test is polymorphic. In Java, all Java objects are polymorphic since they will pass the IS-A test for their own type and for the class Object.
As we know that an object can be accessed through a reference variable.And it solely depends on the “type” of reference variable , which object it can access.A reference variable can refer to an object of its declared type or subtype . A reference variable is only of one type and that “type” cannot be changed.

We can declare a  reference variable as a class or interface type.
While  implementing inheritance, Polymorphism is quite commonly used.

The concepts listed below show different kinds of polymorphism in java.
  • Method Overloading:Two or more methods of same name are defined in one class, in such a way that  there  parameters     are different. This is known as Method Overloading.(Remember : Overloading with parameters) 
  • Method overriding : Declare a method in subclass even though the method is already present in parent class , such that the Child class has  its own implementation for an inherited method    without modifying the method in the parent class/base class . This called Method Overriding . ( Remember : Overriding the implementation)
  Types of polymorphism in java:
  • Compile time Polymorphism or Static Polymorphism
  • Run time Polymorphism or Dynamic Polymorphism 
  •    Compile time Polymorphism or Static Polymorphism :Method Overloading in java is an example of  Compile time polymorphism.Here a class has two or more methods with same name , but there parameter differ in terms of number or type .
  • Run time Polymorphism or Dynamic Polymorphism  :Method Overriding in java is an example of  runtime polymorphism. Here  reference of class  can holds the object of class  or an object of any sub classes .