• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Could you please explain the following code?

 
suresh sai
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends ,here is the scenario due to which iam confusing.

class A
{


}

class B extends A
{


}
class C extends B
{


}

class D
{
void method(A a)
{

System.out.println("in the method A");
}

void method(B b)
{

System.out.println("in the method B");
}

}

class TestOverload
{
public static void main(String l[])
{
C c = new C();
D d = new D();

d.method(c);

}

}

Here,iam overloading the method in class D.Iam expecting a compile time error.Because the compiler is unable to decide which method version to call,because both class A and B can refer to subclass C.But it compiles fine and giving the output as "in the method B".Please explain this.
Thanks in advance.
 
Carl Wauters
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't give a compile error because your methods are correctly overloaded and because it does know which method to call. First it looks for a method with an object of type C as parameter. Since that doesn't exist, it looks at the object hierarchy of c. The first superclass it encounters is class B, and there is a method with type B as parameter, so that one will be chosen.
 
Arundhathi Menon
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its like this - it looks for the object that seems more 'specific' in behaviour.

D Extends C , C Extends B and B Extends A

D shows behaviour closer to C.

This is the way its read. Hence you would not have a compile problem since the compiler would have quite smartly determined who to call
 
Muthukumar Chellappa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The code compiles fine. and give the output "in the method B".
you are correctly overloaded the methods. that is why no problem in the code. now see class A --> class B--> class C--> class D. so it sees the immediate superclass version. But String and StringBuffer classes are same level in the inheritence hierarchy.

Thanks,
C.Muthukumar
 
suresh sai
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Carl Wauters ,Arundhathi,Muthukumar.I got the point from your good knowledge.Thanks once again.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Chinna Suresh,

You can have a look at this thread .


This Selecting the most specific method explains with good examples.
 
suresh sai
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Rhaghavan.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by chinna suresh:
thanks Rhaghavan.


You could have copied-and-pasted my name instead suresh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic