• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Over Riding Variables

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like methods in subclass override methods in superclass, what happens to the variables in subclass when redefined in superclass?

Is there any concept of overriding variables?
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like methods in subclass override methods in superclass, what happens to the variables in subclass when redefined in superclass?

Your statement had interchanged subclass and superclass terms. I think you meant to ask what will happen to the variables of the superclass when redefined in a subclass.

Anyways, They get overrided, in short. But the catch is, the behavior after overriding.

Member Variables are compile time binded with the refernce variables.
Where as Member methods are run time binded to the object that the refernce holds. It will always be better, if you try to work out a program to find out the behavior.




It prints 10 20 10. So for variables, which reference holds the object, is what that decides which value is accessed, unlike overrided member functions.
[ October 29, 2004: Message edited by: Manikandan Jayaraman ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rimzim sinha:
Is there any concept of overriding variables?



Absolutely not!

You can't "override" a variable. If you define a variable in a subclass with the same name as a variable in a superclass, you have "hidden" the superclass variable, not "overridden" it. Overriding is for instance methods only.

Check out the JLS §8.3.3.2 Example: Hiding of Instance Variables.
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corey! One doubt here ... Talking literally, overriding is nothing but hiding ... right???

When we override a base class method in the derived class, we are hiding the base class method from the derived class and its objects. The similar stuff is what that happens with variables too.

If you disagree and have sufficient points to differentiate between "Hiding" and "Overriding", can you please put them forth, here! Thanks!
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manikandan,
In Java overridden methods are by default ( virtual ) meaning Virtual Table is created in memory which holds the virtual pointer to the function which is called at runtime ( Late binding concept ).
Hence when there is code like this
Base objBase = new Subclass();
The compiler defers the binding at run time and it will look at the contents of objBase and not the type of objBase. Here the object contents are of Subclass, so the Subclass version of the method will be called.
As far as variables are concerned, as Corey pointed out there is nothing called overriding variables. Because there is no concept of late binding for variables as described above. The compiler always does the binding for variables at compile time. In short, for VARIABLES always look at the TYPE OF OBJECT and NOT the contents of the object.

NOTE: No VTable is created in memory for holding variables.

Hope this helps you.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hiding is very different from overriding. When you "override" a method, you can utilize polymorphism. When you "hide" something, you do not. Here's an example using static methods and instance methods. Note that instance methods can be overridden while static method are hidden.



Notice how these work. When we invoke a static method, the JVM determines the compile-time type of the variable the method is being invoked upon (regardless of what the run-time type is) and invokes that method. When you invoke an instance method, the JVM first determines what the run-time type of the object is, rather than the type of the reference variable, and invokes that method. This is a polymorphic call.

I would suggest these links for some "light reading":

Static Methods CAN NOT be Overridden
Invoking Static Methods from an Instance
JSL §8.4.6 Inheritance, Overriding, and Hiding
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh... Thanks Corey and Jay! Made me fine with these basics.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Methods are binded at run time.Are final and private methods an exception to this rule?Are they binded at compile time??
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Original post by Atul

Methods are binded at run time.



NOT ALL METHODS are binded at run time ONLY OVERRIDDEN METHODS.

Final and private methods are always binded compile time.

Hope this helps you
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Atul Chandran:
Methods are binded at run time.Are final and private methods an exception to this rule?Are they binded at compile time??



Final method can't be overridden (or hidden) so there is no notion of late-binding with a final method. In essence, a subclass of a class that defines a final method can't have a method with the same name. Therefore, you'd never even some up with a case where you'd have to wonder about this.

Private methods are not inherited so, while you could have a subclass that defines a method with the same name as a method in the superclass, there is no overriding taking place. Private method are, essentially, bound to the compile-time type.
 
rimzim sinha
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you everyone,
 
What are you doing in my house? Get 'em tiny ad!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic