Results 1 to 8 of 8
  1. #1
    ImplicitCharm is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    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.

  3. #3
    ImplicitCharm is offline Member
    Join Date
    Jul 2009
    Posts
    3
    Rep Power
    0

    Default

    So my best bet would be to... just create a new class C from scratch?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by ImplicitCharm View Post
    So my best bet would be to... just create a new class C from scratch?
    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.

  5. #5
    erb26 is offline Member
    Join Date
    Jul 2009
    Location
    Riverside, Ca
    Posts
    1
    Rep Power
    0

    Default

    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?

  6. #6
    fxRichard is offline Member
    Join Date
    Dec 2008
    Posts
    64
    Rep Power
    0

    Default

    Or you can use reflection and change it to what you want, assume object c and private variable w....for example:

    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");
      ...
    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.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by fxRichard View Post
    Or you can use reflection and change it to what you want, assume object c and private variable w....for example:

    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");
      ...
    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.
    You could, however reflection shouldn't be used to get around a percieved problem with a class. That would be bad programming, frankly.

  8. #8
    fxRichard is offline Member
    Join Date
    Dec 2008
    Posts
    64
    Rep Power
    0

    Default

    Correct, my point is if you did not write the class and you absolutely need to change the value of W besides decompiling the code reflection is the most straight forward way.

Similar Threads

  1. Replies: 3
    Last Post: 03-14-2009, 07:22 AM
  2. How to access private data types from public classes?
    By kevzspeare in forum New To Java
    Replies: 3
    Last Post: 03-07-2009, 04:19 AM
  3. Private Classes Clarification
    By justlearning in forum New To Java
    Replies: 1
    Last Post: 05-06-2008, 10:51 PM
  4. [SOLVED] Problem with extending classes...
    By Bizmark in forum New To Java
    Replies: 4
    Last Post: 04-07-2008, 11:21 PM
  5. Question of private member
    By Felissa in forum Advanced Java
    Replies: 2
    Last Post: 06-28-2007, 09:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •