• 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

overloading problem, pls help!

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic