Results 1 to 2 of 2
  1. #1
    thyagu2k8 is offline Member
    Join Date
    Sep 2011
    Posts
    1
    Rep Power
    0

    Question What is the need of dynamic method dispatch?

    Java 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?

  2. #2
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default 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
    Java 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.
    My API:
    Java Code:
    cat > a.out || cat > main.class

Similar Threads

  1. please explain me dynamic methos dispatch
    By ajjukumar in forum New To Java
    Replies: 3
    Last Post: 12-15-2010, 08:11 PM
  2. please explain me dynamic methos dispatch
    By ajjukumar in forum New To Java
    Replies: 3
    Last Post: 12-10-2010, 10:37 AM
  3. Dynamic Method Dispatch
    By sandeshforu in forum New To Java
    Replies: 0
    Last Post: 09-15-2009, 06:18 PM
  4. Dynamic method calling
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 09:46 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •