Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Listener inheritance
    primarykey
    data
    text
    <p>I have a java class which fires custom java events. The structure of the code is the following:</p> <pre><code>public class AEvent extends EventObject { ... } public interface AListener extends EventListener { public void event1(AEvent event); } public class A { public synchronized void addAListener(AListener l) { .. } public synchronized void removeAListener(AListener l) { .. } protected void fireAListenerEvent1(AEvent event) { .. } } </code></pre> <p>Everything works correctly, but I'd like to create a new subclass of A (call it B), which may fire a new event. I'm thinking of the following modification:</p> <pre><code>public class BEvent extends AEvent { ... } public interface BListener extends AListener { public void event2(BEvent event); } public class B extends A { public synchronized void addBListener(BListener l) { .. } public synchronized void removeBListener(BListener l) { .. } protected void fireBListenerEvent2(AEvent event) { .. } } </code></pre> <p>Is this the correct approach? I was searching the web for examples, but couldn't find any.</p> <p>There are a few things I don't like in this solution:</p> <ol> <li><code>BListener</code> has two methods one uses <code>AEvent</code> the other uses <code>BEvent</code> as a parameter.</li> <li><code>B</code> class both has <code>addAListener</code> and <code>addBListener</code> methods. Should I hide addAListener with private keyword? <strong>[UPDATE: it's not possible to hide with private keyword]</strong></li> <li>Similar problem with <code>fireAListenerEvent1</code> and <code>fireBListenerEvent1</code> methods.</li> </ol> <p>I'm using Java version 1.5.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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