• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding Question - Why output is differnet?

 
Sekhar Kadiyala
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why this is printing "AAA" instead of "ABC". By prinint "AAA", it is obvios that compile time reference are still intact and it is not considering the run time behavior. Why?

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);
}}
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a kind of Overload not an Override, parameter type changed.

Invoking these methods depende on the refrence type which is declared as type 'A', hence invoking m1() in class 'A' three times.
 
Mahmoud Kamal
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ahmed Yehia:
This is a kind of Overload not an Override, parameter type changed.

Invoking these methods depende on the refrence type which is declared as type 'A', hence invoking m1() in class 'A' three times.


I don't understand why invoking those methods depend on the reference type at 'run time'?!! As far as I know and understand invoking these methods depends on the runtime object which is A, B and C in our case here.

So can you please explain to me why invoking those methods depend on the reference type?

Thanks in advance for your time
 
Lucy Smith
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the difference between Override and Overload. Pulled straight from KS&B; which overridden version of the method to call is decided at runtime based on the object type, but which overloaded version of the method is called is based on the reference type of the argument passed at compile time.

Just modifying the above code as shown below makes it to print ABC instead of AAA because now it's a override instead of overload.

class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(A b) {System.out.print("B");}}
class C extends B {void m1(A 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);
}}
[ October 29, 2007: Message edited by: Lucy Smith ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15452
42
Android IntelliJ IDE Java Scala Spring
 
Joshua Mark
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing I remember from the K & B book is this....

Over-ride == pass by object type
Overload == pass by reference type

which means when over-riding, you care about the object class itself, when overload (which is what your example is) you care about what the reference variable is, in this case all the reference variables are A, so the methods called are those of class A.

Hope that helps
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joshua Mark:
Another thing I remember from the K & B book is this....

Over-ride == pass by object type
Overload == pass by reference type

which means when over-riding, you care about the object class itself, when overload (which is what your example is) you care about what the reference variable is, in this case all the reference variables are A, so the methods called are those of class A.

Hope that helps


Hi Joshua,

Your description is essentially correct, but you may want to avoid using the terms "pass by object type" and "pass by reference type" in relation to this issue. It's generally better to refer to the mechanisms involved as "compile-time binding" (for overloads) and "runtime binding" (for overrides).

The "pass by reference/object" terminology is more typically used when talking about method argument "pass by reference" semantics vs. "pass by value" semantics, which is a whole different topic. It's kinda pedantic, I know, but one has to be careful with these terms to avoid being tripped up unexpectedly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic