Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Subclass Inheritence for protected variable  RSS feed

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • 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
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Yes Bala.
 
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Sure Venkatasamy. Updated the format:

I am giving code:



 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Venkatasamy.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:Thanks Venkatasamy.


You are Welcome
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!