what is runtime polymorphism or dynamic method dispatch in java

what is runtime polymorphism or dynamic method dispatch in java
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.

Runtime Polymorphism or Dynamic method dispatch



Dynamic method dispatch is a mechanism by which a call to an overridden method is resolved at runtime. This is how Java implements runtime polymorphism. When an overridden method is called by a reference, Java determines which version of that method to execute based on the type of object it refers to. In simple words, the type of object which it referred determines which version of the overridden method will be called.
Runtime Polymorphism or Dynamic method dispatch








Upcasting

When Parent class reference variable refers to Child class object, it is known as Upcasting

Example

class Game
{
 public void type()
 {  System.out.println("Indoor & outdoor"); }
}

Class Cricket extends Game
{
 public void type()
 {  System.out.println("outdoor game"); }

 public static void main(String[] args)
 {
   Game gm = new Game();
   Cricket ck = new Cricket();
   gm.type();
   ck.type();
   gm=ck;      //gm refers to Cricket object
   gm.type();  //calls Cricket's version of type
 }
}
Output :
Indoor & outdoor 
Outdoor game
Outdoor game
Notice the last output. This is because of gm = ck; Now willgm.type() call Cricket version of type method. Because here gm refers to cricket object.

Q. Difference between Static binding and Dynamic binding in java?

Static binding in Java occurs during compile time while dynamic binding occurs during runtime. The static binding uses type(Class) information for binding while dynamic binding uses an instance of the class(Object) to resolve to call of the method at run-time. Overloaded methods are bonded using static binding while overridden methods are bonded using dynamic binding at runtime.

instanceof operator

In Java, the instanceofoperator is used to check the type of an object at runtime. It is the means by which your program can obtain run-time type information about an object. instanceof the operator is also important in case of casting object at runtime. instanceof the operator returns a boolean value if an object reference is of the specified type then it returns true otherwise false.

Example of instanceOf

public class Test
{
    public static void main(String[] args)
    {
      Test t= new Test();
      System.out.println(t instanceof Test);
      }
}
Output :
true

Downcasting

Downcasting








Example of downcasting with the instanceofoperator

class Parent{ }

public class Child extends Parent
{
    public void check()
    {
        System.out.println("Sucessfull Casting");
    }

    public static void show(Parent p)
    {
       if(p instanceof Child)
       {
           Child b1=(Child)p;
           b1.check();
       }
    }
    
    public static void main(String[] args)
    {
      
      Parent p=new Child();
      
      Child.show(p);
      
      }
}
Output :
Sucessfull Casting

More example of an instanceofoperator

class Parent{}

class Child1 extends Parent{}

class Child2 extends Parent{}

class Test
{
  public static void main(String[] args)
  {
      Parent p =new Parent();
      Child1 c1 = new Child1();
      Child2 c2 = new Child2();
      
      System.out.println(c1 instanceof Parent);  //true 
      System.out.println(c2 instanceof Parent);  //true 
      System.out.println(p instanceof Child1);  //false 
      System.out.println(p instanceof Child2);  //false 
      
      p = c1;
      System.out.println(p instanceof Child1);  //true 
      System.out.println(p instanceof Child2);  //false 
       p = c2;
      System.out.println(p instanceof Child1);  //false 
      System.out.println(p instanceof Child2);  //true 
      
   }
    
}
Output :
true
true
false
false
true
false
false
true




















Comments