• 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 wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Are static members inherited?

 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Fellow Ranchers,
I decided that we revisit the puzzle of static members inheritance, I have read lots of articles about this and there seems to be mixed opinion here and there. I believe that static members are inherited, to the best of my knowledge static methods CANNOT be 'polymorphically-overriden' rather they can be 'redefined', we are also aware that runtime-polymorphism does NOT apply to static members.

Further more the only members that obviously CANNOT be inherited are those marked with the private modifier, apart from that, the subclass has access to every other member in accordance with the other rules we know about 'default-package-level-protected-inheritance' and those I may NOT have mentioned herein. Please Ranchers your opinion is needed to resolve this doubt once and for all.

Regards

Ikpefua.
 
Ranch Hand
Posts: 206
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ikpefua Jacob-Obinyan wrote:Hello Fellow Ranchers,
I decided that we revisit the puzzle of static members inheritance, I have read lots of articles about this and there seems to be mixed opinion here and there. I believe that static members are inherited, to the best of my knowledge static methods CANNOT be 'polymorphically-overriden' rather they can be 'redefined', we are also aware that runtime-polymorphism does NOT apply to static members.

Further more the only members that obviously CANNOT be inherited are those marked with the private modifier, apart from that, the subclass has access to every other member in accordance with the other rules we know about 'default-package-level-protected-inheritance' and those I may NOT have mentioned herein. Please Ranchers your opinion is needed to resolve this doubt once and for all.

Regards

Ikpefua.





After research through Sun's Specification documentation I NOW confirmed that static members can inherited by subclasses. Nor in the K&B book or any other source mentioned about this, but Sun Spec does.

My doubt of the result from the codes above that Child class able to calls static "CLASS" member variable led me to the RESEARCH to confirm that STATIC MEMBERS are inherited by by subclass Child despite many other have misunderstood and myself doubted that a subclass just call or able to call static member variable.

Recap from Sun Spec: "Constructors, static initializers, and instance initializers are not members and therefore are not inherited."


See SUN Spec Documentation for more insight: Under section 8.2 Class Members

http://java.sun.com/docs/books/jls/third_edition/html/classes.html

From The Java Language Specification, Third Edition

8.2.1.2 Inheritance with public and protected
Given the class Point:



the public and protected fields x, y, useCount and totalUseCount are inherited in all subclasses of Point.

Therefore, this test program, in another package, can be compiled successfully:






 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler/JVM clarified this doubt for me a longtime ago, but I wondered why many people still claim that static members are NOT inherited.

Good job Tommy Once again you dialogued with codes!...

Regards

Ikpefua.
 
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Funny how I just fluked a correct answer from K&B's Practice Exams book (Self-Assessment Test 2, question 7). I could swear static members weren't inherited, so I went for "Compilation fails". It wasn't the correct reason though, compilation failed because an instance was passed to the super constructor before the instance was created. This question does show a clear case of a subclass inheriting a static variable though...

 
Ranch Hand
Posts: 72
Netbeans IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think we are getting two things confused here. K&B book specifically mentions that static "methods" are not inherited. But since inheritance concerns only instance methods and not instance variables or static members variables. It is easy to conclude that that static variables are another form of a "variable" hence the access modifier will play a big role (if it is private we cannot use it in subclass, if it is public then we can and so on)...
it is even easier to understand if instead of inherited static members we use "accessed" static members.

 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Boris Mechkov wrote:I think we are getting two things confused here. K&B book specifically mentions that static "methods" are not inherited. But since inheritance concerns only instance methods and not instance variables or static members variables. It is easy to conclude that that static variables are another form of a "variable" hence the access modifier will play a big role (if it is private we cannot use it in subclass, if it is public then we can and so on)...
it is even easier to understand if instead of inherited static members we use "accessed" static members.


Hello Boris couldnt it be that you are the one getting into the "mixed up" here? please kindly indicate the page where the K&B book specifically mentions that static members are NOT inherited.
The first point of correction is that inheritance concerns EVERY visible member of a super-class including static members, instance methods are the ONLY members involved in runtime-polymorphism...And yes I repeat (RUNTIME-POLYMORPHISM).
The K & B book in chapter 2 page 91 says inheritance in java are created by extending a class, to promote code reuse and to use polymorphism...My question to you is if you CANNOT inherit static members then what is the code reuse promotion here?...ONLY instance variables AND methods?...hmmm well I guess you should take a second thought.
To further explain static members and instance variables are NOT involed in runtime-polymorphism (here is that expression again runtime-polymorphism)...Pop quiz what do I mean by runtime-polymorphism?...I will appreciate your response, I am trying to make you understand the difference between inheritance and polymorphism.

Summary:

-Static members are inherited
-They are NOT involved in runtime-polymorphism
-You can 'redefine' static methods (They CANNOT be overriden)
-instance variables are NOT involved in runtime-polymorphism
-it is ONLY instance methods that are involved in runtime-polymorphism

Regards

Ikpefua
 
reply
    Bookmark Topic Watch Topic
  • New Topic