| Author |
Accessing Super class Private Member from its Sub class
|
Parth Krishnan
Greenhorn
Joined: Apr 19, 2009
Posts: 6
|
|
|
How to access private data member of SUPER class from its SUB class?
|
cheers,
Parth - parthme@live.com
|
 |
Ryan Beckett
Ranch Hand
Joined: Feb 22, 2009
Posts: 192
|
|
|
A subclass only inherits non-private superclass members. See this for more info on inheritance.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
 |
|
|
subject: Accessing Super class Private Member from its Sub class
|
|
|