• 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
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

class inheritance and static variable question

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

From the <<SCJP 5 Study Guide>> book Page 162, Qn 11.
In case you don't have the book, here is the code:



This code compiles fine.

I am really confused by the access to 'y' in Minor's constructor( the line super(y); ), how do you explain it?
My understanding is, y is static in Uber class and is not inherited in class Minor 'coz it is static. but if I change it to super(Minor.y);, it still compiles.

anyone please enlighten me? Thanks.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not an advanced Java question.

I guess that the two classes are in the same package. Since "y" has no access specifier, it has package access. Therefore, it can be accessed by any class in the same package.

Being static does not affect the inheritance of "y".
 
warren li
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so you mean static variables/methods are also inherited?
this is rather contrary to my belief.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by warren li:
so you mean static variables/methods are also inherited?
this is rather contrary to my belief.



Well, you'd best update your belief by reading the language specs or a good book! The access to static fields and methods is controlled by their access specifier, exactly like member fields and methods.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static field is inherited too.
But there will be 2 different copies in the parent and child classes.

Kevin Huang
SCJP, SCWCD, SCBCD
MCP, MCAD, MCSD
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kevin Huang:
Static field is inherited too.
But there will be 2 different copies in the parent and child classes.

Kevin Huang
SCJP, SCWCD, SCBCD
MCP, MCAD, MCSD



There is only one copy and it belongs to the Class, not any particular instance.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic