Runtime Polymorphism

  • May 25, 2022
  • Java
method overloading and method overriding
Polymorphism:

Polymorphism is a Java concept that allows us to perform a single action in multiple ways. Polymorphism is made up of two Greek words: polymorphism and morphism. The words “poly” and “morphs” both mean “many.” Polymorphism denotes the presence of multiple forms.

Types of polymorphism:

  • compile-time polymorphism
  • runtime polymorphism.

Polymorphism in Java is achieved through method overloading and overriding.

Runtime Polymorphism in Java:

A call to an overridden method is resolved at runtime rather than compile-time in runtime polymorphism or Dynamic Method Dispatch.

In this process, a super class’s reference variable is used to call an overridden method. The object referred to by the reference variable is used to determine which method should be called.

Upcasting:

Upcasting occurs when a reference variable in the Parent class refers to an object in the Child class. For instance:

class A{}  
class B extends A{}  
A a=new B();//upcasting  

We can use the reference variable of class type or an interface type for upcasting. For Example:

interface I{}  
class A{}  
class B extends A implements I{}  

Here, the relationship of B class would be:

B IS-A A
B IS-A I
B IS-A Object

Since Object is the root class of all classes in Java, so we can write B IS-A Object.

Example:
class Car{  
  void run(){System.out.println("running");}  
 }  
class Honda extends Car{  
  void run()
 {
  System.out.println("running safely with 60km");
 }  
  
  public static void main(String args[]){  
    Car b = new Honda();//upcasting  
    b.run();  
  }  
}  
Output:
running safely with 60km.
Runtime polymorphism: shapes example
class Shape
{  
  void area()
   {
     System.out.println("area of a shape:");
   }  
}  
class Rectangle extends Shape
{  
   void area()
     {
       System.out.println("area of a rectangle:l*w");
     }  
}  
class Circle extends Shape
{  
   void area()
   {
     System.out.println("area of a circle: pi*r*r");
   }  
}  
class Triangle extends Shape
  {  
    void area()
    {
      System.out.println("area of a triangle:(b*h)/2");
    }  
}  
class TestPolymorphism2
 {  
    public static void main(String args[])
  {  
   Shape s;  
   s=new Rectangle();  
   s.area();  
   s=new Circle();  
   s.area();  
   s=new Triangle();  
   s.area();  
 }  
}  
Static binding & dynamic binding
  • Binding is a mechanism that connects the method call and the actual implementation of the method.
  • A Java object can have many forms thanks to the polymorphism concept.
  • At both compile and run time, object forms can be resolved.
  • Static binding occurs when the link between the method call and the method implementation is resolved at compile time, whereas dynamic binding occurs when the link is resolved at run time.
  • Static binding uses the type of the class and fields to resolve binding, whereas dynamic binding uses the object.
Sr. No.KeyStatic BindingDynamic Binding
1Basic It is resolved at compile time It is resolved at run time 
2        Resolve mechanism  static binding use type of the class and fieldsDynamic binding uses object to resolve binding 
3Example Overloading is an example of static binding Method overriding is the example of Dynamic binding 
4.Type of Methods private, final and static methods and variables uses static bindingVirtual methods use dynamic binding 

Note: also read about the Method Overriding in Java

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Reply

Your email address will not be published. Required fields are marked *