Results 1 to 8 of 8
- 07-23-2009, 07:36 AM #1
Member
- Join Date
- Jul 2009
- Posts
- 3
- Rep Power
- 0
OOP Question re. private variables and extending classes
Hi All,
Perhaps this is a silly question, but here goes:
I'm working with a class (let's call it Class C) from a library that I'm importing in. C has many variables that are public, but there's one that's private in there (Widget W).
My issue is that Widget W is initialized in a certain way that I'm not fond of, and this is causing issues for me. I'd ideally like a version of Class C exactly as it is except for that one line where Widget W is initialized to be dictated by me instead.
What's the best way for me to do this? Can I do it at all, given that W is private?
Thanks!Last edited by ImplicitCharm; 07-23-2009 at 07:39 AM.
- 07-23-2009, 11:51 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
It's private.
The author of C didn't want anyone mucking about with W directly, hence making it private. You run the risk of breaking something should you manage to get around this.
- 07-23-2009, 04:40 PM #3
Member
- Join Date
- Jul 2009
- Posts
- 3
- Rep Power
- 0
So my best bet would be to... just create a new class C from scratch?
- 07-23-2009, 04:49 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Well, if C doesn't actually do what you want it to do then that would be the solution. But make sure that C doesn't in fact do what you want it to, and you haven't simply misunderstood something...that is you're not trying to shoehorn it into whatever solution you've come up with, wheras youcould still use it if you modified your solution.
- 07-24-2009, 04:58 AM #5
Member
- Join Date
- Jul 2009
- Location
- Riverside, Ca
- Posts
- 1
- Rep Power
- 0
I am a beginning programmer that has a contructor already built. The problem is that the constructor has nothing to construct. There is no seperate class to call, and I cannot figure out how to link the two together. Any feedback?
- 07-25-2009, 03:42 AM #6
Member
- Join Date
- Dec 2008
- Posts
- 64
- Rep Power
- 0
Or you can use reflection and change it to what you want, assume object c and private variable w....for example:
This is just an example and assumes the Field w is a string when it sets it with a string value on the last line however it should be enough to get you started.Java Code:import java.lang.reflect.*; ... Object c = someObjectYouCreated; Field w = c.getClass().getDeclaredField("w"); //where "w" = the actual field name w.setAccessible(true); w.set(c,"mynewValue"); ...
- 07-28-2009, 03:12 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
- 07-28-2009, 03:46 PM #8
Member
- Join Date
- Dec 2008
- Posts
- 64
- Rep Power
- 0
Similar Threads
-
Private or Protected access for super class variables
By Madushan in forum New To JavaReplies: 3Last Post: 03-14-2009, 07:22 AM -
How to access private data types from public classes?
By kevzspeare in forum New To JavaReplies: 3Last Post: 03-07-2009, 04:19 AM -
Private Classes Clarification
By justlearning in forum New To JavaReplies: 1Last Post: 05-06-2008, 10:51 PM -
[SOLVED] Problem with extending classes...
By Bizmark in forum New To JavaReplies: 4Last Post: 04-07-2008, 11:21 PM -
Question of private member
By Felissa in forum Advanced JavaReplies: 2Last Post: 06-28-2007, 09:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks