Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing Super class Private Member from its Sub class

 
Parth Krishnan
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?
 
Ryan Beckett
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic