This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Accessing Super class Private Member from its Sub class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Accessing Super class Private Member from its Sub class" Watch "Accessing Super class Private Member from its Sub class" New topic
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
    
  19


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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing Super class Private Member from its Sub class
 
Similar Threads
Can a private method be overridden by a subclass ?
Inheritance
Possible error in K&B book (resolved - no error)
A question regarding the protected member access
What is your understanding of "Casting" ?