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 Dan's questions : overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic
Author

Dan's questions : overloading

Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
Question 23
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 c1 = new C(); C c2 = new C(); c1.m1(c2);
}}
What is the result of attempting to compile and run the program?
a. Prints: A
b. Prints: B
c. Prints: C
d. Compile-time error
e. Run-time error
f. None of the above
Answer is : prints A
I thought it was C because I've read that with overloading the compiler looks at the reference to see what method should be invoked..
c1.m1(c2);
Here the reference c2 is of type C so the compiler will invoke the method in class C, no ?
I thought that it was during overiding that the Object type was looked at during runtime but here we are talking about overloading methods... Guess I'm confused...
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Kristof,
The reference type of c1 is A, and type A only declares one method named m1. The methods that overload method name m1 are declared in classes B and C, but those methods can not be invoked using a reference of type A.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
Ok Dan I got it ! Thanks
One more question concerning this :
Would it be answer c if class A had overloaded methods m1 with types B & C ?
Kristof
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
Yes Kris, you can try it with the code as well.


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dan's questions : overloading