Forums Register Login

Overload or Override?

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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:
+Pie Number of slices to send: Send
Yes, what Joel said!
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
thank you all. i appreciate your help with such in depth look at in how overload and override work.thank you!!!
+Pie Number of slices to send: Send
With other words: instance variables aren't polymorphic.
Doesn't this belong into the FAQ...?
+Pie Number of slices to send: Send
 

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.
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1046 times.
Similar Threads
super.painComponent(g) not clearing my Jpanel
Syntax for mapping a var to a method
Question On Comparator
Serializable
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 22:58:50.