File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding Question - Why output is differnet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding Question - Why output is differnet?" Watch "Overriding Question - Why output is differnet?" New topic
Author

Overriding Question - Why output is differnet?

Sekhar Kadiyala
Ranch Hand

Joined: Feb 17, 2004
Posts: 170
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);
}}


PMP CSQA SCJP SCWCD SCBCD INS 21 INS 23
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
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

Joined: Oct 22, 2007
Posts: 5
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

Joined: Aug 07, 2007
Posts: 17
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

Joined: Aug 16, 2005
Posts: 14269
    
  21

Note: Please quote your sources.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Joshua Mark
Greenhorn

Joined: Sep 23, 2007
Posts: 15
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

Joined: Oct 20, 2007
Posts: 513
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.


SCJP 5.0
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Overriding Question - Why output is differnet?