Friday 8 July 2016

What is Dynamic Method Dispatch?

Method overriding is a disadvantageous fact, because in this concept only sub class property is coming as output, super class property is hidden. So to eliminate this disadvantage we need run-time polymorphism which is also known as dynamic method dispatch.
  • Dynamic method dispatch is a mechanism in which a call to an overriden method is reserved at runtime rather than compile time.
  • This is a mechanism that represents how java implement runtime polymorphism.
  • In this mechanism a super class reference variable can refer to a sub class object.
  • When an overriden method is called through a super class reference, java determines which method is executed and this is happened at runtime.
Example:

class Test
{
void display()
{
system.out.println("JavaRace");
}
}
class Test1 extends Test
{
void display( )
{
system.out.println("Silan Software");
}
}
class Test2 extends Test
{
void display( )
{
system.out.println("Silan Technology");
}
}
class Dispatch {
public static void main(String[] args)
{
Test obj1=new Test( );
Test1 obj2=new Test1( );
Test2 obj3=new Test2( );
Test r;
r=obj1;
r.display();
r=obj2;
r.display();
r=obj3;
r.display();
}
}

Output
JavaRace
Silan Software
Silan Technology 

No comments:

Post a Comment