• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Overload method

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following example is from Dan's mock exam.
What's the output of 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);
}
}
The answer is AAA. I don't understand it. Isn't at runtime c2 will be bound to type C and thus the result should be ABC? Please help me.
Thanks.
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My understanding:
As object c2 is of type A it is bound to its method m1 at compile time and hence you get AAA because all the 3 c2 objects are of type A.Try coding this example using different references like B c2 and C c2 and you will be able to see the difference.
La Vish
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
La Vish is correct. Object c2 is of type A and therefore does not know about the overloaded methods declared in subclasses B and C.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do clarify
as per my understanding
1) c2 is a reference of type A
2) the object is of type C and c2 points to it.

A c2 ;
c2 = new C();
is the same as

A c2 = new C();
thanx in advance
sonu
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sonu
Both are the same thing. Its just that the former one is the long form of doing the same thing the later one does.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the selection of method was based on reference, why the below example prints AAB, instead of
AABB. That means, if the most suitable method(with out implicit conversion) is availble then the
method call is dispatched to that class in the hierarchy. Am i right? Pls. explain..thx.
class A {
void m1(A a) {System.out.print("AA");}
void m1(B b) {System.out.print("AB");}
void m1(C c) {System.out.print("AC");}
}
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 D16 {
public static void main(String[] args) {
A c1 = new C();
A c2 = new C();
B b1 = new B();
c1.m1(c2);
c1.m1(b1);
}
}
[ May 28, 2003: Message edited by: Samy Venkat ]
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
void m1(B b) {System.out.print("B");}
overrides the m1 method from class A.
So when you call
B b1 = new B();
c1.m1(b1);
"B" will be printed rather than "AB"
Overloaded method are resolved at compile time.
(Object type is used at compile time)
Overridden methods are resolved at run time.
(reference is used at run time)
reply
    Bookmark Topic Watch Topic
  • New Topic