• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static and Inheritance - strange java...why is this happening ?  RSS feed

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read the code :


I was expecting the output as :

the static variable bar of parent is -> PARENT !!!
the static variable bar of child is -> CHILD !!!
____________________________________________
But what i get is :

the static variable bar of parent is -> PARENT !!! CHILD !!!
the static variable bar of child is ->
____________________________________________
LOOK ! nothing after child ->
So, how to get the desired output ? Child inherits bar() OF Parent, right ? so why is that not called ?


 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,
first of all the output is


Secondly the Static variables are shared by all the instances of the class. Due to this when you print "c.bar", you get to see the EMPTY string as output.
When you call c.bar("Child"), this calls the parent class's method and modifies the parent's class's static variable.

Lastly the subject should mention "STATIC" instead of "STATICS". Statics is a different concept that is not Java.

Regards,
Amit
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amit punekar wrote:......

Made the changes you mentioned. Can i get the output i was expecting, instead of the one i am getting ?

thanks.
 
amit punekar
Ranch Hand
Posts: 544
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
You can get the desired output by
1) Converting the bar() method to static
2) Add same method in the Child class as well.

Well, what I did not understand here is whether getting the desired output ( for which there are other ways than mentioned above as well) Or are you trying to learn a concept??

regards,
Amit
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote:
So, how to get the desired output ?




Rahul Sudip Bose wrote:Child inherits bar() OF Parent, right ? so why is that not called ?


bar() method is belong to Parent class.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote: . . . bar() method is belong to Parent class.
It is not overridden or anything. Remember that static members of a class do not take part in polymorphism, so the bar() method is applied to the Parent#bar field. Try overriding the method by copying it unchanged.

By the way: there are several pieces of poor practice there:
  • Using the terms "parent" and "child". It's superclass and subclass; using parent and child is misleading, however many people do it.
  • You ought to invoke static methods on the name of the class
  • Fields not marked as private.

  • Obviously this is a "let's see what happens if . . ." example, but it should have taught you not to mix inheritance and static members. Having two static members with the same name in superclass and subclass is a recipe for disaster.
     
    Rahul Sudip Bose
    Ranch Hand
    Posts: 637
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    amit punekar wrote:Hi
    You can get the desired output by
    1) Converting the bar() method to static
    2) Add same method in the Child class as well.

    Well, what I did not understand here is whether getting the desired output ( for which there are other ways than mentioned above as well) Or are you trying to learn a concept??

    regards,
    Amit


    Thanks ! that worked. I was trying to get that output using this program itself. What are the other ways,other than the ones you mentioned ?
     
    Rahul Sudip Bose
    Ranch Hand
    Posts: 637
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    there are several pieces of poor practice there:[list]Using the terms "parent" and "child". It's superclass and subclass; using parent and child is misleading, however many people do it.

    Please let me know how this can be misleading for others ?
    Thanks for showing me the errors. Is there any book from which i can get some tips or learn a few good practices in design and coding ?
     
    Debashis jain
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @ sudip bose
    In java the class from where we derive new classes is primarily known as the 'superclass',and the derived class is known as the 'subclass'.
    parent and child concept is mostly used in the c++ language
    That's why using of child and parent is not supported.
     
    Campbell Ritchie
    Marshal
    Posts: 56584
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Welcome to the Ranch

    It is because "parent" and "child" are used for GUI components inside other GUI components. And because biological and genetic inheritance works completely differently from computing inheritance. It's because you don't have a parent, you have parents, two of them, and in Java™ you only have one superclass at a time.

    And if you search, you find this, and this, and lots more.
     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Java Windows XP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Seetharaman Venkatasamy wrote: . . . bar() method is belong to Parent class.
    It is not overridden or anything. Remember that static members of a class do not take part in polymorphism, so the bar() method is applied to the Parent#bar field.


    even, I meant what you said here!
     
    Campbell Ritchie
    Marshal
    Posts: 56584
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Seetharaman Venkatasamy wrote: . . . even, I meant what you said here!
    Yes, you did. But I thought a bit more explanation would help.
     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Java Windows XP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote: I thought a bit more explanation would help.

     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!