Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance and static members

 
j rab
Greenhorn
Posts: 16
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain to me why a static member in a base class can be accessed by calling the child class if static members aren't inherited? For example this code was on a mock exam:
 
Julian West
Ranch Hand
Posts: 89
3
Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They aren't *inherited*, which means they aren't overridden but hidden by subclass static members.

https://docs.oracle.com/javase/tutorial/java/IandI/override.html
 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
j rab wrote:Can someone explain to me why a static member in a base class can be accessed by calling the child class if static members aren't inherited?

  • Assuming that these classes are in a package.
  • static Or class variable belongs to class means there exists only one copy of static variable, separate static variable will not be created for each objects of that class.
  • In your example we have a static member which is a field having package-private access modifer in super class named Super which can be inherited in subclasses Sub.
  • Because here we have only one static String ID in Super class which can be inherited in subclass Sub. If subclass Sub also had static String ID in It then ID of Super class might not have been inherited in Sub because ID of Sub would have hidden ID of Super.

  • Example:
    Output:
    In Parent class: Parent-idOne
    idOne in Child class: Child-idOne
    idTwo in Child class: Parent-idTwo
    idOne Of Parent in Child class: Parent-idOne

  • All the classes are in a package.
  • Subclass Child inherited idTwo of superclass Parent.
  • idOne of Parent class is not inherited in Child class because the declaration of idOne in Child class hides the definition of idOne in Parent class.
  •  
    j rab
    Greenhorn
    Posts: 16
    Eclipse IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you.
     
    Ganesh Patekar
    Bartender
    Posts: 694
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    j rab wrote:Can someone explain to me why a static member in a base class can be accessed by calling the child class if static members aren't inherited?

    Which statement made you conclude that static members are not inherited? Static members in the superclass that are visible to the subclass are inherited (as illustrated in your code snippet). But (and that could be the reason for you confusion): static (class) methods are never overridden. Never! Repeat after me: static (class) methods are never overridden Only instance methods can be overridden (if the instance method is inherited). So that means, a private instance method can never be overridden as well.
    A subclass can define a static method with the same signature as a static method in the superclass. That's called hiding: the method in the subclass hides the one in the superclass. Did you know the rules for method overriding apply to these hiding static methods as well, although they can't be overridden? Read this post to find out more.

    The difference between overriding and hiding is one of the very common topics in this forum, so you'll find plenty of explanations and code examples. In this topic you'll find a very good explanation. And like I said, it's very common so using the search function you'll find plenty of topics:
  • Overriding static methods
  • overriding Interface variables
  • Question about static or private method hidden/redefined in subclass
  • Confused about static and instance methods with same name (with an excellent explanation about which rules you have to follow when hiding a static method)
  • Exceptions, overriding and constructors


  • Hope it helps!
    Kind regards,
    Roel
     
    prateek shaw
    Ranch Hand
    Posts: 32
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,

    Thanks for asking this question. Everyone has explained what you asked for. But this program has something more interesting.



    And i do not thing that anyone explained about that. If we look at this code we will see that we are accessing static ID variable of class Super. However if you pay close attention then you can see that Sub class also has static Block.
    I thought when we run the program it will print

    In SubQBANK


    Why thought this , since we are accessing Sub class so it does make sense that class loader to load this class.But it is not , it will print only.
    QBANK
    .

    I read the explanation, but i was not able to understand , Could some explain this ?
     
     
    Henry Wong
    author
    Marshal
    Pie
    Posts: 22104
    88
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    prateek shaw wrote:
    Why thought this , since we are accessing Sub class so it does make sense that class loader to load this class.But it is not , it will print only.
    QBANK

    I read the explanation, but i was not able to understand , Could some explain this ?


    The compiler is smart enough to understand that it doesn't actually need the subclass. The generated code goes directly to the reference in the super class only.

    Interestingly, if you make the variable final, it will fulfill the last requirement of the variable being a compile time constant. And the compiler is smart enough to understand that it doesn't actually need the superclass either. The value will be directly coded into the Test class.

    Henry
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    prateek shaw wrote:I read the explanation, but i was not able to understand , Could some explain this ?

    This topic contains a great explanation about the same code snippet. And in this topic and this one you'll find some additional information about static initializer blocks and the order of execution. All definitely worth reading!

    Hope it helps!
    Kind regards,
    Roel

     
    prateek shaw
    Ranch Hand
    Posts: 32
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for your reply.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic