Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing private field in a subclass
    text
    copied!<p>I'm creating a class that extends <code>PropertyChangeSupport</code>. What I currently want to do is override <code>firePropertyChange()</code>:</p> <p>firePropertyChange as it is implemented in PropertyChangeSupport:</p> <pre><code>public void firePropertyChange(String propertyName, Object oldValue, Object newValue) { if (oldValue == null || newValue == null || !oldValue.equals(newValue)) { firePropertyChange(new PropertyChangeEvent(this.source, propertyName, oldValue, newValue)); } } </code></pre> <p>my intended override of <code>firePropertyChange</code>:</p> <pre><code>public void firePropertyChange(String propertyName, Object oldValue, Object newValue) { if (oldValue == null || newValue == null || !oldValue.equals(newValue)) { firePropertyChange(new JoystickPropertyChangeEvent(this.source, propertyName, oldValue, newValue)); //compile error: source is not visible } } </code></pre> <p><code>JoystickPropertyChangeEvent</code> is a class that I created and that extends <code>ProperyChangeEvent</code>.</p> <p>The problem is that my intended implementation does not compile because source is private and has no <code>getters</code> in <code>PropertyChangeSupport</code>, so subclasses have no access to it. I cannot modify <code>PropertyChangeSupport</code>'s code.</p> <p>Is there a more elegant way of solving this than having a private copy of <strong>source</strong> as a field of my subclass?</p> <p>Related question: <a href="https://stackoverflow.com/questions/2258891/how-to-access-the-private-variables-of-a-class-in-its-subclass">How to access the private variables of a class in its subclass?</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload