• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Subclass Inheritence for protected variable

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

Subclass will inherit super class instance variable through inheritence but not through object reference. But subclass can inherit super class static variable through inheritence or object reference. Please let me know if I am wrong. But is Kathy Sierra book, author gave generic statement about the variables.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but not through object reference



What does that mean ? That the variable will not be inherited if you have a reference to it ?

This should be simple enough to try. Create a sub class and super class and introduce 4 variables with different access modifiers. Do they get inherited ?
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static variables can be accessed in subclass through inheritance and object reference. instance variables can be accessed through inheritence only but not through object references.
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:Static variables can be accessed in subclass through inheritance and object reference. instance variables can be accessed through inheritence only but not through object references.



What do you mean by object references ? That you have a reference to the object ?
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Bala.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:Hi,

Subclass will inherit super class instance variable through inheritence but not through object reference. But subclass can inherit super class static variable through inheritence or object reference. Please let me know if I am wrong. But is Kathy Sierra book, author gave generic statement about the variables.


Could you please elaborate a bit? I couldn't understand you wordings!
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably, Harikrishna is taking about calling the protected member of superclass[which is in different package] by superclass object reference in subclass.

<edit>protected members are available to the subclass via IS-A relationship, not a HAS-A relationship</edit>

Am I right Harikrishna ?
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure Venkatasamy. Updated the format:

I am giving code:



 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my guess is right. read my first post

<edit> hari, It is always better to use code tag, while posting your code. so that it can be easy to read and will attract people to see your post</edit>
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Venkatasamy. IS-A is fine for both instance/static variables/methods. HAS-A is fine for static variables/methods and not for instance variables/methods as per the code given above. Is that right (or) Am I doing/missing something wrong ??
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are confusing with instance and static variable, methods. It is about visibility. I suggest you to read about AccessModifiers
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Venkatsamy, I am not confused. What I mean to ask is, Protected = package level + subclasses in different packages for IS-A relationship. If subclass has HAS-A relationship to Super class only static variables/methods are visible and instance variables/methods are NOT visible..Is that correct ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:Hi Venkatsamy, I am not confused. What I mean to ask is, Protected = package level + subclasses in different packages for IS-A relationship.


right!

Harikrishna Gorrepati wrote:
If subclass has HAS-A relationship to Super class only static variables/methods are visible and instance variables/methods are NOT visible..Is that correct ?


Yes. if the variable/method is *protected*.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Venkatasamy.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:Thanks Venkatasamy.


You are Welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic