简体   繁体   中英

handleEvent method in EventListener interface not implemented in the implementing class

EventListener interface declares handleEvent(Event evt) method, and in the code below GeneratorListener extends that interface. I am told this code is correct. BUT I don't see why Printer class does not have to implement handleEvent method? Isn't it the case that all the methods in an interface must be implemented?

public interface GeneratorListener extends EventListener {
    void objectGenerated(String object);
}

public class Printer implements GeneratorListener { 
    public void objectGenerated(String object) {
        System.out.println(object);
    }
}

Seems I know the answer. Method

public void handleEvent(Event evt);

is contained in org.w3c.dom.events.EventListener. But in your GeneratorListener interface you probably imported java.util.EventListener that looks like

package java.util;

/**
 * A tagging interface that all event listener interfaces must extend.
 * @since JDK1.1
 */
public interface EventListener {
}

So your Printer class implements the only abstract method that you have in Printer class hierarchy. That's why your code is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM