Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Static Variable of superclass in subclass.....Inheritance?

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all....

I am sorry for creating a new thread as there are similar threads exisitng but my concept is still not clear .

I have a self test question no 11 from SCJP 5 K&B book that dint understand.

Class Uber -has a static variable -y
Child Class Minor- is accessing this static variable y of super class ,directly without using . operator (Uber.y) in its constructor.

Class Uber{
statiic int y=2;
Uber(){}
Uber(int x){}
....

}
Class Minor extends Uber{
Minor()
{
super(y);.... //shouldnt program break here
....}

}
As per my knowledge,Statics -belong to Class and not inherited ,not overridden but redefined.
So we are not even redefining the variable .
How is Minor class able to access the static var of Super-class as its own static variable?


Please suggest me the possbile reason for this..



 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lucky
I think that you have misunderstood what 'extends' does.

Basically when you use 'extends' the class takes on all the properties of the Uber class in addition to its' own, so the reason 'super.' is not needed in front of 'y' is because the class Minor has its' own instance of y.

For simplicity you could think of extends as a flag to tell the compiler to copy the contents of the Uber class into the Minor class without overwriting anything (which then gives you your overrides).

James
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The example is a bit strange, but to see if it works, put yourself in the role of the compiler:

In the first line of the class Minor constructor, your code calls a superclass constructor that takes a single argument, to which you are passing "y". Is there a variable "y" in scope at that point? Yes -- it's a static variable introduced in the superclass. That variable is of type int, therefore, "super(y)" is an attempt to call a constructor in the superclass that takes a single argument of type int. Is there such a constructor in the superclass? Yes. Therefore, everything compiles.

It'll run too -- if you had code that said "new Minor();", that call would branch to the Minor constructor, which would pass the value of "y" to the Uber constructor that takes a single parameter of type int. That constructor doesn't do anything with the passed value, which is one of the odd things about the example. It's also odd to pass the value in "y" up to the ancestor. But I guess whoever made up that example was trying to get you to think about scope.
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you all for the explanation.

I read from some prev threads -that unless subclass hides the statis variable, it is the same static variable referenced by all the subclasses.


if in my example
Minor declares new static variable (means re-defining /hiding old)

int static y=20;

Class Minor extends Uber{
Minor(){
super(y);.... //here y=20 belonging to Minor
}

}


Please Confirm to what i understood -
So Superclass Static variables are not 'ACTUALLY' inherited but remains in the scope until 'RE DEFINED' by the subclass.
 
Sheriff
Posts: 22509
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lucky, please use code tags.
 
reply
    Bookmark Topic Watch Topic
  • New Topic