File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overload method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overload method" Watch "Overload method" New topic
Author

Overload method

Yuan Ye
Ranch Hand

Joined: Mar 05, 2003
Posts: 172
The following example is from Dan's mock exam.
What's the output of following code:
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 {
public static void main(String[] args) {
A a1 = new A();
B b1 = new B();
C c1 = new C();
A c2 = new C();
c2.m1(a1);
c2.m1(b1);
c2.m1(c1);
}
}
The answer is AAA. I don't understand it. Isn't at runtime c2 will be bound to type C and thus the result should be ABC? Please help me.
Thanks.
La Vish
Ranch Hand

Joined: Apr 17, 2002
Posts: 158
My understanding:
As object c2 is of type A it is bound to its method m1 at compile time and hence you get AAA because all the 3 c2 objects are of type A.Try coding this example using different references like B c2 and C c2 and you will be able to see the difference.
La Vish


La Vish
SCJP 1.4, President 60s Club
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
La Vish is correct. Object c2 is of type A and therefore does not know about the overloaded methods declared in subclasses B and C.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
sonu sharma
Greenhorn

Joined: May 17, 2003
Posts: 24
do clarify
as per my understanding
1) c2 is a reference of type A
2) the object is of type C and c2 points to it.

A c2 ;
c2 = new C();
is the same as

A c2 = new C();
thanx in advance
sonu
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Sonu
Both are the same thing. Its just that the former one is the long form of doing the same thing the later one does.
Samy Venkat
Greenhorn

Joined: May 26, 2003
Posts: 10
If the selection of method was based on reference, why the below example prints AAB, instead of
AABB. That means, if the most suitable method(with out implicit conversion) is availble then the
method call is dispatched to that class in the hierarchy. Am i right? Pls. explain..thx.
class A {
void m1(A a) {System.out.print("AA");}
void m1(B b) {System.out.print("AB");}
void m1(C c) {System.out.print("AC");}
}
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 D16 {
public static void main(String[] args) {
A c1 = new C();
A c2 = new C();
B b1 = new B();
c1.m1(c2);
c1.m1(b1);
}
}
[ May 28, 2003: Message edited by: Samy Venkat ]
Ryan Wilson
Ranch Hand

Joined: Apr 16, 2003
Posts: 65
void m1(B b) {System.out.print("B");}
overrides the m1 method from class A.
So when you call
B b1 = new B();
c1.m1(b1);
"B" will be printed rather than "AB"
Overloaded method are resolved at compile time.
(Object type is used at compile time)
Overridden methods are resolved at run time.
(reference is used at run time)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overload method
 
Similar Threads
Qn from dan exam
inheritance question
Polymorphisam
overloading problem, pls help!
Could any one of you explain me this code