Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.
Let’s understand through below code…
class tata { public void car() { System.out.println(“The brand is TATA”); } } class Nexon extends tata { public void car() { System.out.println(“The car model is -> Nexon”); } } class tiago extends tata { public void car() { System.out.println(“The car model is -> tiago”); } } class cars{ public static void main(String[] args){ tata c1 = new tata(); // creating object ‘c1’ of class ‘tata’ c1.car(); tata c2 = new Nexon(); c2.car(); tata c3 = new tiago(); c3.car(); } } |

superclass called ‘tata’ that has a method called car(). Subclasses of ‘tata’ are tiago,nexon and they also have their own implementation of method car().
Polymorphism Types
There are two types of polymorphism in java –
1. Compile-time polymorphism
2. Run-time polymorphism
Let’s understand these concepts one by one below…
- Compile-time polymorphism
It is also known as static polymorphism. This type of polymorphism is achieved by function overloading.
Let’s see below code…
class TATA{ static void car(int x,int y){ System.out.println(“x is -> “+x); System.out.println(“y is -> “+y); } static void car(float x,float y){ System.out.println(“x is -> “+x); System.out.println(“y is -> “+y); } } class methodOver{ public static void main(String[] args){ TATA t1 = new TATA(); t1.car(100,200); t1.car(12.3f,20.5f); } } |

Let’s see another code…
class A{ static int run(int a,int b){ int c=a+b; return c; } static float run(float a,float b){ float c=a+b; return c; } } class methodOver2{ public static void main(String[] args){ A a1 = new A(); System.out.println(a1.run(10,20)); System.out.println(a1.run(10.3f,20.6f)); } } |

2. Run-time Polymorphism: It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime. This type of Polymorphismis achieved by Method Overriding.
Let’s understand through below code…
class parent{ public void display(){ System.out.println(“parent class”); } } class child1 extends parent{ public void display(){ System.out.println(“child1 class”); } } class child2 extends parent{ public void display(){ System.out.println(“child2 class”); } } class methodOverRide{ public static void main(String[] args){ parent p1 ; p1 = new child1(); p1.display(); p1 = new child2(); p1.display(); } } |

Let’s see another code…
class dbsir{ public void role(){ System.out.println(“this is debasish sir and he is H.O.D”); } } class rcmam extends dbsir{ public void role(){ System.out.println(“C,DS using C,OS,DBMS,TOC”); } } class dcmam extends dbsir{ public void role(){ System.out.println(“discreate maths,soft. engg,python”); } } class mcmam extends dbsir{ public void role(){ System.out.println(“microprocessor,digital electronics,multimedia”); } } class methodOverRide2{ public static void main(String[] args){ dbsir tchr; tchr = new rcmam(); tchr.role(); tchr = new dcmam(); tchr.role(); tchr = new mcmam(); tchr.role(); } } |
