Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overload or Override?

 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
i got confused about whether the method setX(5) bellow in the code is an override or an overload?
can you tell me with explanations?


thanks.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it's both. There are two overloaded versions of setX() in SubClass -- one that takes one argument, the other takes two.
One of these two methods -- the single-argument one, the one that you call as setX(5) -- overrides the implementation of setX(int) in Base.
Note that if SubClass had ONLY the two-argument setX(), then it would still be said that SubClass had two overloaded versions of setX(). A method is only said to override another method if the two have precisely identical names, argument count, and argument types.
 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Namaste Sathi:
hi all,


why print(b.x) prints 0 when i do
my understanding is that at runtime, b will be referencing an object of SubClass and thus print(b.x) will be 3 but it's the otherway. could anyone plase explain me this?
thanks.
 
Arvind Varma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Namasthe Sathi,
The foolowing code u put shud haven given u a compile error...
***************

***************************
The simple reason being : Sub class can be akind of Base class but a Base Class cannot be a kind of Sub class....
so by specifying
you are basically creating an instance of Base Class (since there is no constructor defined for the SubClass in ur code)...so therefore a base class is created and hence u get a value of '0' for b.x
 
Gayathri Prasad
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sathi,
Yeah !! Arivind is correct.. and this concept is called the Scoping of Objects.When You assign a derived class's instance to the Base class the base class object can refer only to that part of code which is similar to the base n derived but not which is unique to derived class.Thus by assigning a derived class object to the base class you are limiting the scope of the object.
Cheers,
Gaya3
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Namaste --
Sorry, but Arvind is quite wrong here; Gayathri is a bit closer but I'm not sure he's really got it. I can correctly explain what you're seeing. First, it is always possible to assign an instance of a subclass to a variable declared to refer to a superclass; the whole concept of polymorphism rests on this. Thus assigning an instance of SubClass to an instance of Base is perfectly fine, and the mostnatural thing in the world in Java programming.
But the problem is (and I didn't notice this when I replied previously) is that both Base and SubClass have a member variable "x", and that's NOT the most natural thing in the world. In fact, it's something that you should generally avoid, always, because it's quite confusing. Variables don't override one another, they hide one another; here the x in SubClass hides the x in Base. You get the surprising result you're seeing for the following reason. Imagine that variable "o" refers to a SubClass object (the type of o doesn't matter.) Now, the expression ((Base) o).x
refers to the x in the Base part of the object, while the expression ((SubClass) o).x refers to the other x, the one declared in the subclass. In your design, you definitely intend for the two classes to share a single declaration of x -- the one in the subclass should be removed. If you did, you'd see the result you were expecting!
[ July 12, 2003: Message edited by: Ernest Friedman-Hill ]
 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i appreciate the way you explain. thank you. however, i still have difficulty when you say the following.
Originally posted by Ernest Friedman-Hill:

Variables don't override one another, they hide one another; here the x in SubClass hides the x in Base. [ July 12, 2003: Message edited by: Ernest Friedman-Hill ]

i totall agree when you say that the same variable name hides the same vaiable in superclass and they don't override each other. if so why

prints 0. like you said, the variable x in b which is SubClass should hide the variable x in Base and supposed to print the value of x in SubClass which is 3. but it looks like the variable x in SubClass is not hiding the variable x in Base but still referring to the Variable x in Super Class and printing 0 instead. am i right?
please explain me again.
thank.s
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason that you are seeing 0 being printed is that which version of x is used is not determined at runtime but rather at compile time. Method/variable shadowing (a.k.a., hiding) is resolved at compile-time. Since there are two possibilities for x, the compiler determined which on to use based on the reference type (not the actual object type). The actual object b is going to be a SubClass, but the compiler has no way of knowing that at compile time. It does know that the Reference is to a Base class, so it uses Base.x instead of SubClass.x
It is possible to shadow methods as well; static methods are never overridden, only shadowed. Also, private methods are never overridden, just shadowed. (This can be quite annoying if you forget, or don't notice your method is private)
Compare the printMessages:
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, what Joel said!
 
Arvind Varma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,
what u said is true..i sound incorrect from ur point of view...i shud have explained my point more clearly..

Sub class can be a kind of Base class but a Base Class cannot be a kind of Sub class....
i was actually referring to scoping of objects and i sure i'm not wrong in that statement
you are basically creating an instance of Base Class (since there is no constructor defined for the SubClass in ur code)...so therefore a base class is created and hence u get a value of '0' for b.x
in this statement i was trying to address the problem from compiler point of view...the same was stated by Joel later...but i went completely off the track by talking abt the constructor...
and finally the most important of allThe foolowing code u put shud haven given u a compile error...
my sincere apologies..i didn't go thro the code correctly...
But i appreciate you pointing it and i'll make it sure that i explain stuff rather than jus answering to the point ...
Thanks
Arvind
[ July 12, 2003: Message edited by: Arvind Varma ]
 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all. i appreciate your help with such in depth look at in how overload and override work.thank you!!!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With other words: instance variables aren't polymorphic.
Doesn't this belong into the FAQ...?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
With other words: instance variables aren't polymorphic.
Doesn't this belong into the FAQ...?

I was thinking that this belongs as a tatoo on the right arm of every Java programmer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic