• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading

 
Anjali Bahl
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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

Thanks
 
Akshatha Nayak
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anjali

http://www.coderanch.com/t/246076/java-programmer-SCJP/certification/Overloading

ur problem has been solved here ..
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic