Results 1 to 4 of 4
  1. #1
    ajjukumar is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Lightbulb please explain me dynamic methos dispatch

    Can any one explain me how the following code executes::

    class A
    {

    void m1(A a)
    {
    System.out.println("A");
    }

    }



    class C extends A
    {

    void m1(C a){
    System.out.println("C");
    }

    }

    class Dmd
    {

    public static void main(String args[])
    {
    A ac = new C();
    C c = new C();
    ac.m1(c);
    }
    }

    OUTPUT:: A

    i am ajay doing b.tech .
    hello in the above example i am sending reference c as argument and i though the output will be "C" but unfortunately it was "A"
    i am unable to understand what is happening can any one say me or mail me to
    ajaytummapala@gmail.com
    Thanks in advance...

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    The two methods m1( ... ) are overloaded; there is no overriding here; because class A doesn't have a method m1(C x) and because a C is an A the method m1( ... ) in class A will be called.

    b.t.w. I know of at least one person here who gets extremely itchy when he reads the word 'dynamic'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ajjukumar is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    The two methods m1( ... ) are overloaded; there is no overriding here; because class A doesn't have a method m1(C x) and because a C is an A the method m1( ... ) in class A will be called.

    b.t.w. I know of at least one person here who gets extremely itchy when he reads the word 'dynamic'.

    kind regards,

    Jos

    thanks jos for ur answre but can u explain me in much clear manner i am unable to get ur point..please
    regards ajay

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Please don't use txt spk.
    There's no need for it you know.

    And I really wouldn't recommend posting your email address on an open forum like that.

Similar Threads

  1. Interface usage and the double-dispatch pattern
    By tashimoto in forum New To Java
    Replies: 0
    Last Post: 12-08-2010, 01:34 AM
  2. Request dispatch from one web app to another
    By karthikus in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 06-15-2010, 07:31 PM
  3. Dispatch simulated events
    By kusanagi97 in forum AWT / Swing
    Replies: 1
    Last Post: 10-12-2009, 02:20 PM
  4. Dynamic Method Dispatch
    By sandeshforu in forum New To Java
    Replies: 0
    Last Post: 09-15-2009, 05:18 PM
  5. Error: Exception during event dispatch!
    By Javid in forum AWT / Swing
    Replies: 1
    Last Post: 08-02-2008, 01:20 PM

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
  •