• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing Super class Private Member from its Sub class  RSS feed

 
Greenhorn
Posts: 6
Eclipse IDE Firefox Browser Windows Vista
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to access private data member of SUPER class from its SUB class?
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A subclass only inherits non-private superclass members. See this for more info on inheritance.
 
Sheriff
Posts: 21201
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try reflection, but if you run into this problem it generally means a) you shouldn't access that private member, or b) your parent class is badly designed for inheritance.
 
author
Sheriff
Posts: 23329
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In my opinion, this is actually common. When someone designs a class, they are thinking... "This variable is used for internal calcs, it should be private" or "This variable should be exposed, so I'll make it public, or provide a getter". I don't anyone thinks... "This variable will be used by a subclass, that I'll need next year, so I'll make it protected".

In my opinion, it may be best to go back and refactor the superclass.

Henry
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!