aspose file tools*
The moose likes Beginning Java and the fly likes Could you please explain the following code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Could you please explain the following code?" Watch "Could you please explain the following code?" New topic
Author

Could you please explain the following code?

suresh sai
Ranch Hand

Joined: Jul 16, 2007
Posts: 62
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

Joined: Aug 02, 2007
Posts: 19
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.


SCJP 5.0, SCWCD 1.4, SCBCD 5.0, SCJD, SCDJWS 5.0, SCEA/OCMJEA 5
Arundhathi Menon
Ranch Hand

Joined: Jan 14, 2004
Posts: 113
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


SCJP , SCWCD , SCBCD , SCDJWS
Muthukumar Chellappa
Greenhorn

Joined: Aug 02, 2007
Posts: 9
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

Joined: Jul 16, 2007
Posts: 62
Thank you Carl Wauters ,Arundhathi,Muthukumar.I got the point from your good knowledge.Thanks once again.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Hello Chinna Suresh,

You can have a look at this thread .


This Selecting the most specific method explains with good examples.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
suresh sai
Ranch Hand

Joined: Jul 16, 2007
Posts: 62
thanks Rhaghavan.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Originally posted by chinna suresh:
thanks Rhaghavan.


You could have copied-and-pasted my name instead suresh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Could you please explain the following code?