File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
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: 19790
    
  20

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19072
    
  40


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