File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloading" Watch "Overloading" New topic


Anjali Bahl

Joined: Aug 05, 2004
Posts: 7

Please check out the 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);

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

Correct answer is : AAA

This is a case of overloading , so it is the reference type which determines the method to be invoked.
Now c2.m1(a1) will return A
c2.m1(b1) ?? here the reference arguement is of type B
c2.m1(c1) ?? here the refernce arguement is of type C

Akshatha Nayak
Ranch Hand

Joined: Jul 15, 2004
Posts: 53

ur problem has been solved here ..

Only those who will risk going too far, Can possibly find out how far one can go! <br />happiness is journey and not destination<br /> <br />A Nayak<br />----------------------------------<br />SCJP 1.4<br />SCWCD 1.3<br />SCBCD 1.3
I agree. Here's the link:
subject: Overloading
It's not a secret anymore!