• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question abt overloading frm Mock Test

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...

 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
m1() is overloaded, NOT overridden. And when it comes to overloaded methods, it is the method of the reference type that is called.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Piy,
This link (last message) may be perhaps helpfull:
https://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:
https://coderanch.com/t/242444/java-programmer-SCJP/certification/overriding-overloading
Hope this helps,
Cyril.
 
reply
    Bookmark Topic Watch Topic
  • New Topic