Dynamic Method Dispatch or Run Time Polymorphism

Posted By on September 16, 2014


Download PDF
Difference between string class and string buffer class:
Static Keyword

Dynamic Method Dispatch

Method overriding forms the basis for one of Java’s most powerful concepts: dynamic method dispatch. Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. Dynamic Method Dispatch is related to a principle that states that an super class reference can store the reference of subclass object. However, it can’t call any of the newly added methods by the subclass but a call to an overridden methods results in calling a method of that object whose reference is stored in the super class reference.

It simply means that which method would be executed, simply depends on the object reference stored in super class object.

upcasting:
When parent class reference variable refers to child class object ,it is called upcasting

 

Example for Dynamic Method Dispatch

 

class A 
{
void callme() 
{
System.out.println(“Inside A’s callme method”);
}
}
class B extends A 
{
void callme()   // override callme()
{
System.out.println(“Inside B’s callme method”);
}
}
class C extends A 
{
 void callme() // override callme()
{
 System.out.println(“Inside C’s callme method”);
}
public class Dynamic_disp 
{
public static void main(String args[]) 
{
 A a = new A(); // object of type A
 B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A
r = a; // r refers to an A object
r.callme(); // calls A’s version of callme
r = b; // r refers to a B object
r.callme(); // calls B’s version of callme         
r = c; // r refers to a C object
r.callme(); // calls C’s version of callme
}
}

This would produce the following results

Inside A’s callme method
Inside B’s callme method

Inside C’s callme method

Here reference of type A, called r, is declared.The program then assigns a reference to each type of object to r and uses that reference to invoke callme( ).As the output shows, the version of callme( ) executed is determined by the type of object being referred to at the time of the call.
Difference between string class and string buffer class:
Static Keyword

Download PDF

Posted by Akash Kurup

Founder and C.E.O, World4Engineers Educationist and Entrepreneur by passion. Orator and blogger by hobby

Website: http://world4engineers.com