Dynamic method Dispatch

Posted By on October 2, 2014


Download PDF
String Buffer class in java
Applet Basics

We know that polymorphism is the feature of object oriented programming.

In c++, there was a concept of virtual function and pure virtual function through which we can gain polymorphism.

In java, Dynamic method dispatch provides polymorphism. The basic principle here  is that “access the subclass object using reference of  parentclass”.

Through this we can obtain run_time polymorphism.

Dynamic method dispatch is the  mechanism  by which  a call to an overridden method is resolved at run time, rather than compile time.

class A {
void callme() {
System.out.println(“Inside A’s callme method”);
}
}
class B extends A {
// override callme()
void callme() {
System.out.println(“Inside B’s callme method”);
}
}
class C extends A {
// override callme()
void callme() {
System.out.println(“Inside C’s callme method”);
}
}

 

class Dispatch {
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
}
}

output::

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

String Buffer class in java
Applet Basics

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