aspose file tools*
The moose likes Beginning Java and the fly likes subclass calling private variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "subclass calling private variable" Watch "subclass calling private variable" New topic
Author

subclass calling private variable

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Simple question.

Is there an easy way round a subclass calling a PRIVATE variable from it's superclass?

The superclass is from a OOB system and I am not allowed to change it (hence the need for the subclass) but to run the subclass I need to call some variables (mainly a boolean) from the superclass.

Is there a way round it?

Cheers

KS
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
The short answer is, no.

The long answer is, if they intended to allow access from subclasses they should provide public get and set methods, or some other method allowing users to alter the values.

BTW: Please don't use abbreviations like OOB whihc aren't familiar to users, especially not on the beginners' forum.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

The correct short answer is no, except through some hacks. Unless prevented by the security manager, reflection allows you to do such things.

Keep one thing in mind though: there is most likely a reason the fields are private, and if there are no public or protected methods to change them, you basically should not touch them. You will more likely break tested and proven behaviour of the base class than "fix" it for your own purposes.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Rob Prime:

Keep one thing in mind though: there is most likely a reason the fields are private, and if there are no public or protected methods to change them, you basically should not touch them. You will more likely break tested and proven behaviour of the base class than "fix" it for your own purposes.


True.

On the other hand, there really *are* very rare situations where you really *need* access to something private, there simply is no other way using the provided API *and* there really is no better other way to work around the limitation. I've done this once or twice in my career, always felt uneasy about it, but still think that it was the best I could do in that situation.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
wood burning stoves
 
subject: subclass calling private variable