Dynamic Method Dispatch Assignment Help

Assignment Help: >> Polymorphism in Java - Dynamic Method Dispatch

Dynamic method dispatch

Certainly, it would be, at best, an interesting curiosity, but of little real value if there were nothing more to method overriding than a name space convention. Moreover, this is not the case. A Method overriding forms the basis for one of Java's most powerful concepts that is: dynamic method dispatch.

Dynamic  method  dispatch  is  the  mechanism through  that  a  call  to  an  overridden function is resolved at run time, relatively compile time. A Dynamic method dispatch is very important since this is how java implements run-time polymorphism.

Let's starts though restating an important principle: a super class reference variable could refer to a subclass object. A Java uses this fact to resolve calls to overridden methods at run time. Here is how. Whenever an overridden method is called by a superclass reference, A Java determines that version of that method to execute based upon the category  of  the  object  being  referred  to  at  the  time  the  call  occurs.  Therefore, this determination is made at run time. Whenever various types of objects are referred to, various versions of an overridden method will be called. Alternatively, it is the type of the object being referred to that determines that version of an overridden method will be executed. Thus, it a superclass holds a method which is overridden through a subclass, then whenever various types of objects are referred to by a superclass reference variable, various versions of the method are executed.

Here is an instance which describes dynamic method dispatch:

// Dynamic method dispatch

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()

System.out.println("Inside C's callme method");

}

}

class Dispatch {

public static void main(String args[]){

A a = new A();  // objects of type A

B b = new B();           // Objects of type B

C c = new C();  // objects of type C

A r; //Obtain a reference of type A

r=a; r.callme();

r=b; r.callme();

r=c; r.callme();

}

}

The output from the program is display here:

Inside A's callme method

Inside B's callme method

Inside C's Callme method

That  program creates one superclass called A and two subclasses of it, called B and C. A Subclasses B and C override callme() define in A. Within the main () method, objects of type A, B, and C are defined. Moreover, a reference of type A, called r, is defined. The program then assigns a reference to every category of objects to r and uses which reference to  invoke  callme()  As  the  output  displays  the  version  of  callme()  executed  is determined through the type of object being referred to at the time of the call. Had it been determined via the type of the reference variable, r, you will see three calls to A's callme() method.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd