Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method invocation doubt

 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A
{
void m1(A a)
{
System.out.print("A");
}
}
class B extends A
{
void m1(B b)
{
System.out.print("B");
}
}
class C extends B
{
void m1(C c)
{
System.out.print("C");
}
}
class D extends C
{
void m1(D d)
{
System.out.print("D");
}
public static void main(String[] args)
{
A a1 = new A();
B b1 = new B();
C c1 = new C();
D d1 = new D();
d1.m1(a1); //1
d1.m1(b1);
d1.m1(c1);
}
}

Here the Output is ABC
The doubt is
when d1.m1(a1) //1 is called .....then the method in D class should be called.
Why is it calling method in class A?
 
agrah upadhyay
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here the Output is ABC
The doubt is
when d1.m1(a1) //1 is called .....then the method in D class should be called.
Why is it calling method in class A?


Class D is extending class C which extending to B which further extending to class A.Means class D is AVAILABLE with methods:void m1(A a) ,void m1(B b),void m1(C c),void m1(A a) .
Now based upon method argument call,method invokation is resolved.
Got It?
 
Andreas Sandberg
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooops I almost confused myself with this problem. I think you're under the impression that what is happening here is that the method is being overridden but if you look closely you will see that the argument list does not exactly match. Each subclass modifies the argument list ever so slightly. So D actually has all of the methods from class A,B, and C. It would be no different than if you did this.



So c.m1(3,3) will call the method that was defined in class B. The same is true for your example except the argument list is a single object but not the same object so overriding does not take place.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Whats the explanation for this??

c1.m1(d1);

Output: C

Tx
 
agrah upadhyay
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
posted September 28, 2005 03:56 AM
Hi ,

Whats the explanation for this??

c1.m1(d1);

Output: C

My previous post should explain it.For class C only 3 overloaded versions are available:m1(A a),m1(B b),m1(C c).Now c1 is an instance of
class C.So compiler has to resolve what function to be called
in invocation c1.m1(d1); .Now compiler has to choose
more specific method with respect to argument .Remember C is direct superclass of D.so output will be
C means m1(C c) has been called.
 
Arvind Giri
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

Hi ,

Whats the explanation for this??

c1.m1(d1);

Output: C


Hint : it is calling most specific method

Hope this gives u a diretion to think
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic