aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overloading problem, pls help! 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 problem, pls help!" Watch "overloading problem, pls help!" New topic
Author

overloading problem, pls help!

mari krishna
Greenhorn

Joined: Feb 13, 2003
Posts: 10
Hi all,
These ex are from Dan Chisholm site
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();
B c2 = new C();
C c3 = new C();
C c4 = new C();
c4.m1(c1);
c4.m1(c2);
c4.m1(c3);
}
}
The ans: ABC
Explanation : One of the overloaded methods is selected based on the type of the argument. The type of the argument is determined by the reference type. The type of c1 is A. The type of c2 is B. The type of c3 is C.
Based on the above ans and explanation i moved to the next ques which is as follows:

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();
A b1 = new B();
A c1 = new C();
C c4 = new C();
a1.m1(c4);
b1.m1(c4);
c1.m1(c4);
}
}

Based on the above ans and explanation, i thought the ans would be CCC, but i was wrong and the ans is AAA, and the following ques confused me more,
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);
}
}
ans: AAA
I guess, i'm missing something here, please help!
thanks in advance for ur replies
mari
Kaz Yosh
Ranch Hand

Joined: May 22, 2003
Posts: 63
This might help you understand this.
class A {
void m1(A a) {System.out.print("A");}
}
class B extends A {
void m1(B b) {System.out.print("B");}
//m1 method inherited from class A
void m1(A a){System.out.print("A");
}
class C extends B {
void m1(C c) {System.out.print("C");}
//m1 method inherited from class A and B
void m1(A a) {System.out.print("A");}
void m1(B b) {System.out.print("B");}
}


"If anything can go wrong, it will"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overloading problem, pls help!