What is the need of dynamic method dispatch?
Code:
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
What is the need of the above code if we can call the method using the objects a, b and c?
Re: What is the need of dynamic method dispatch?
I am not too sure what the point of that is. I would assume that callme() is a function within the object 'a','b', and 'c'. There really is no need to have a reference to any of the functions in this particular example because all you have to do is
Code:
a.callme();
b.callme();
c.callme();
I assume this practice is to show you how you can use one object to reference another but I personally have never needed to use this.
I could see this being useful if you need to obtain a certain method that a program cannot get its hands on and must use an method reference in order to get it. I cannot think of an example where you would need this though.