To listen or to override

?

-

If a method in a superclass changes the value of a property and a
subclass needs to do something upon the change, do I have to override
the superclass' method, do something, and then call the superclass'
method, or should I register a PropertyChangeListener?
 
C

Chris Head

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

- - said:
If a method in a superclass changes the value of a property and a
subclass needs to do something upon the change, do I have to override
the superclass' method, do something, and then call the superclass'
method, or should I register a PropertyChangeListener?

Hi,
Either method works, technically. I would think that if you're trying to
listen for a property change, a PropertyChangeListener is the way to go
(it just seems to me that's the whole purpose of the system, so why not
use it?)

Subclassing just to get notification when one property is changed could
get messy I would think, especially if two people both decide to do it,
then one has to subclass the other's code if both want to be notified,
and so on.

Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32)

iD8DBQFCtEIpgxSrXuMbw1YRAuNfAJ0dTTodZ1OJQE/IkS9rYdGOFkru0QCbBruM
upXvONrYQcJcNsp7T/b7/II=
=aePh
-----END PGP SIGNATURE-----
 
R

Raymond DeCampo

Chris said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1




Hi,
Either method works, technically. I would think that if you're trying to
listen for a property change, a PropertyChangeListener is the way to go
(it just seems to me that's the whole purpose of the system, so why not
use it?)

Subclassing just to get notification when one property is changed could
get messy I would think, especially if two people both decide to do it,
then one has to subclass the other's code if both want to be notified,
and so on.

But if I read the OP correctly, he already has a subclass. So
essentially the subclass wants to know when the object's own attributes
are changed. Using a listener system seems kind of overkill for that.

Ray
 
C

Chris Head

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Raymond DeCampo wrote:
[snip]
But if I read the OP correctly, he already has a subclass. So
essentially the subclass wants to know when the object's own attributes
are changed. Using a listener system seems kind of overkill for that.

Ray


Hi,
I didn't actually notice that the first time (the subclass is already
there); however... I guess it depends. Look at a JFrame after all: you
subclass it, but you still stick listeners in it for WindowEvents. On
the other hand, if it's something more simple like properties changing,
then I agree catching the modifications in an overridden method might be
less work.

Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32)

iD8DBQFCtHQ3gxSrXuMbw1YRAqrPAJ0WwYUOLDMiYFvAv1xR2MD/bdqvhgCg08TR
ophnM2YTbdiSklaErLsOKpQ=
=SxRh
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top