Java default interface methods concrete use cases

后端 未结 5 868
广开言路
广开言路 2020-12-30 09:01

Java 9 is near to come and more features will be added to Java interfaces, like private methods. default methods in interfaces were added in Java 8, essentially

5条回答
  •  庸人自扰
    2020-12-30 09:38

    Removing the Default-Adapter of Listener by using Default Methods

    Sometimes, we need to introduce a default Adapter class for the java.util.EventListener which has multiple events need to trigger, but we only interested in some of events. for example: swing create each *Adapter class for each *Listener.

    I recently found this is very useful when we declare listeners with default methods we can remove the middle adapter class. for example:

    interface WindowListener extends EventListener {
    
        default void windowOpened(WindowEvent e) {/**/}
    
        default void windowClosing(WindowEvent e) {/**/}
    
        default void windowClosed(WindowEvent e) {/**/}
    }
    

提交回复
热议问题