In this article,Dynamic Method Dispatch is explained in detail with examples.
you will know about what is Dynamic method dispatch in the java program and why this method is also known as dynamic polymorphism,is clearly explained with different Examples.
This concept in Core Java is related to dynamic polymorphism (Dynamic Method Dispatch) it is implemented through overriding in multi-level inheritance.
Here using the object reference variable of a top-level base class,we invoke overriding methods in respective sub-class,when base-class object reference variable contain sub-class object reference codes.This mechanism is known as "dynamic polymorphism" because JVM (Java Virtual Machine) knows which overriding method need to be invoked from sub classes only during execution time.you will know about what is Dynamic method dispatch in the java program and why this method is also known as dynamic polymorphism,is clearly explained with different Examples.
This concept in Core Java is related to dynamic polymorphism (Dynamic Method Dispatch) it is implemented through overriding in multi-level inheritance.
(From topic Dynamic Method Dispatch)
Ex
//program related to Dynamic Method Dispatch.
class A
{
void m1()
{
System.out.println("Class A m1()");
}
}
class B extends A
{
void m1()
{
System.out.println("Class B m1()");
}
}
class C extends B
{
void m1()
{
System.out.println("Class C m1()");
}
}
class D extends C
{
void m1()
{
System.out.println("Class D m1()");
}
}
class Dispatch
{
public static void main(String args[])
{
A a; //object ref variable of top-level base class.
a=new A();
a.m1();
a=new B();
a.m1();
a=new C();
a.m1();
a=new D();
a.m1();
}
}
Output
Class A m1()
Class B m1()
Class C m1()
Class D m1()
Ex
//program related to Dynamic Method Dispatch.
class A
{
void call()
{
System.out.println("Class A Call()");
}
}
class B extends A
{
void call()
{
System.out.println("class B Call()" ); //(From topic Dynamic Method Dispatch)
}
}
class C extends B
{
void call()
{
System.out.println("Class C call()");
}
}
class Dispatch1
{
public static void main(String args[])
{
A a=new A();
B b=new B();
C c=new C();
A r;
r=a;
r.call();
r=b;
r.call();
r=c;
r.call();
}
}
Output
Class A call()
Class B call()
Class C call()
Note
This concept in C++ is known as run-time polymorphism and is implemented through virtual functions. (From topic Dynamic Method Dispatch)
Continue to next topic Thread in Java.
Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.
Do check my new startup Surprise Planners in Hyderabad- Lavish Surprises our services are surprise party planners in Hyderabad, surprise gifts, surprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.
Hi Friends, Please comment down your views in the comment section below. Thank you...
No comments:
Post a Comment