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

    Default 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
    Thanks in advance...

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

    Default

    I already answered that exact same question in this thread.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Same code, but fleshed out a little more:
    Java Code:
    class Animal
    {
    
    	void eat(Animal a)
    	{
    		System.out.println("I'm eating an Animal.");
    	}
    	
    	void legs() {
    		System.out.println("I may not have legs at all.");
    	}
    
    }
    Java Code:
    public class Biscuit {
    	// doesn't do anything -- just food
    }
    Java Code:
    class Cat extends Animal
    {
    
    	void eat(Cat a){
    		System.out.println("I'm eating a Cat.");
    	}
    
    	void eat(Biscuit b) {
    		System.out.println("I'm eating a Biscuit.");
    	}
    	
    	void legs() {
    		System.out.println("I have four legs.");
    	}
    
    }
    Java Code:
    class Demonstration
    {
    	public static void main(String args[])
    	{
    		Animal ac = new Cat();
    		Biscuit b = new Biscuit();
    		Cat c = new Cat();
    		ac.eat(c);
    		// ac.eat(b); -- can't do this! Animal has no eat(Biscuit) method! so...
    		((Cat) ac).eat(b);
    		((Cat) ac).eat(c);
    		ac.legs();
    		c.eat(c);
    		c.eat(ac);
    		c.eat(b);
    		c.legs();
    	}
    }
    Output:

    I'm eating an Animal.
    I'm eating a Biscuit.
    I'm eating a Cat.
    I have four legs.
    I'm eating a Cat.
    I'm eating an Animal.
    I'm eating a Biscuit.
    I have four legs.

    It's still somewhat surprising, but it starts to make sense with the additional examples.

    -Gary-

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    I already answered that exact same question in this thread.

    kind regards,

    Jos
    I didn't see that before I posted. OP, it's not nice to waste people's time with cross-posting.

    -Gary-

Similar Threads

  1. Replies: 2
    Last Post: 12-13-2010, 01:34 PM
  2. Replies: 3
    Last Post: 12-13-2010, 07:22 AM
  3. please explain me dynamic methos dispatch
    By ajjukumar in forum New To Java
    Replies: 3
    Last Post: 12-10-2010, 09:37 AM
  4. 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
  5. Dynamic Method Dispatch
    By sandeshforu in forum New To Java
    Replies: 0
    Last Post: 09-15-2009, 05:18 PM

Posting Permissions

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