Categories: Java

Runtime Polymorphism

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

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago