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 important because this is how Java implements run-time polymorphism.
·       By restating an important principle: a super class reference variable can refer to a subclass object. Java uses this fact to resolve calls to overridden methods at run time. Here is how. When an overridden method is called through a super class reference, Java determines which version of that method to execute based upon the type of the object being referred to at the time the call occurs. Thus, this determination is made at run time. When different types of objects are referred to, different versions of an overridden method will be called.
·       In other words, it is the type of the object being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed. Therefore, if a superclass contains a method that is overridden by a subclass, then when different types of objects are referred to through a superclass reference variable, different versions of the method are executed.
·        Here is an example that illustrates 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()
  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


Why multiple inheritance is not supported in java?

 Why multiple inheritance is not supported in java?

To reduce the complexity and simplify the language, multiple inheritance is not supported in java.  Consider a scenario where A, B and C are three classes. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there will be ambiguity to call method of A or B class. Since compile time errors are better than runtime errors, java renders compile time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error now. 
For example:
class A{
void msg(){
System.out.println("Hello");
}
   }
   class B{
    void msg(){
System.out.println("Welcome");
}
   }
   class C extends A,B{//suppose if it were
        public static void main(String args[]){
        C obj=new C();
        obj.msg();//Now which msg() method would be invoked?
    }
   }   

How to crack Aptitude Question In GATE and many Competitive Examination

As you know the aptitude in the competitive examination is quite for more challenging to clear it. But believe me it is the easy stuff t...