This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question abt overloading frm Mock Test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question abt overloading frm Mock Test" Watch "question abt overloading frm Mock Test" New topic
Author

question abt overloading frm Mock Test

piy varsh
Greenhorn

Joined: May 19, 2003
Posts: 7
hi,
following question frm danchisholm mock test which says...
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);
}
}

What is the result of attempting to compile and run the program?
a. Prints: AAA
b. Prints: ABC
c. Prints: CCC
d. Compile-time error.
e. Run-time error.
f. None of the above.

ans given is a.) Prints AAA
In my understanding it should give comile time error since c2 object has refernce of class C so it should execute the method of class C and in class C method m1 can not accept object of Class A since super class's ref. can not be assigned to subclass without casting.
have I got it correctly? Please share your views...

Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
m1() is overloaded, NOT overridden. And when it comes to overloaded methods, it is the method of the reference type that is called.
Mahendra Deshpande
Greenhorn

Joined: Dec 22, 2000
Posts: 14
I think Alton is right. The methods are overloaded.
But then why compiler does not complian at statement
c2.m1(b1);
Because of relationship of B IS-A A ( B inherits from A ).
Thus, at compile time b1 is upcasted to A, thus compiler
finds method m1 in A which accepts object of type A as
a argument.
The simliar case happens at statement c2.m1(c1);
Have I got that right ?
[ August 02, 2003: Message edited by: Mahendra Deshpande ]
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
Hi Piy,
This link (last message) may be perhaps helpfull:
http://www.coderanch.com/t/242443/java-programmer-SCJP/certification/inheritance
I asked myself the same question.
Remember that for method overloading, at compile time, in a call of the form x.f(...) where x is supposed to be of class X, choice of f is determine as follows:
compiler determines in class X or any superclass of X the signature of the best method f corresponding to the call, what defines in the same time the return type.
So, in your example:

only method m1 of class A is accessible, because c2 is of type A.
If you change the code to the following one, making c2 of type C:

you will get ABC as output, because c2 has now the three overloaded methods at its disposition (by extending B and A, any instance of class C owns these overloaded methods), then the compiler may choose the 'best' one.

More difficult (overloading + overriding):

output: C.A
Explanations:
http://www.coderanch.com/t/242444/java-programmer-SCJP/certification/overriding-overloading
Hope this helps,
Cyril.


SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
 
Don't get me started about those stupid light bulbs.
 
subject: question abt overloading frm Mock Test