What is the difference between dynamic method dispatch and method over riding?

Showing Answers 1 - 12 of 12 Answers

Dynamic dispatch is about delegating the method of an object during run-time. To look a bit deeper, it more addresses programming to an interface.

Say you have an interface being realized by two different classes. e.g
Interface A containing a method signature, say exampleMeth(), is realized (or implemented) by Class A and Class B as well.

public interface A{
    public void exampleMeth();
}

class B implements A{
    public void exampleMeth(){
        System.out.println("Printed in class B");
    }
}

class C implements A{
    public void exampleMeth(){
        System.out.println("Printed in class C");
    }
}

now,,,
public class ExecuteExample{
 
    public static void main(String arg[]){

          Interface objDyn = new B();
          objDyn.exampleMeth();   /* here it dynamically delegates to the method contained  in class B*/

          
          objDyn = new C();
         objDyn.exampleMeth(); /* here it dynamically delegates to the method contained  in class C*/

    }
}

--------
Coming to method over-riding, simply saying the same method (along with its signature) is redefined in the inherited class.

To make a difference with method overloading, this is polymorphic representation of the same method (only in the name, but different in its signature i.e, the return type and type and number of arguments would be different) within the same class.

Hope am clear.

Regards,
Uresh Kuruhuri

  Was this answer useful?  Yes

sampra

  • Feb 11th, 2008
 

Assigning subclass object to superclass ref var at run tim is called dynamic dispath

Method of superclss implemented in subclass  is method overriding

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions