This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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.
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.
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
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
author and iconoclast
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 ]
Joined: Oct 26, 2002
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
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:
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
author and iconoclast
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 ]
Joined: Oct 26, 2002
thank you all. i appreciate your help with such in depth look at in how overload and override work.thank you!!!
With other words: instance variables aren't polymorphic. Doesn't this belong into the FAQ...?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus