Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I override only specific methods

I'm currently working on a java me application and I have this code:

svgForm.setSVGEventListener(new SVGEventListener(){
                public void keyPressed(int i) {
                    System.out.println("Val"+i);
                }
                public void keyReleased(int i) {
                    System.out.println("Val"+i);
                }
                public void pointerPressed(int i, int i1) {
                    return;
                }
                public void pointerReleased(int i, int i1) {
                    return;
                }
                public void hideNotify() {
                    return;
                }
                public void showNotify() {
                    return;
                }
                public void sizeChanged(int i, int i1) {
                    return;
                }
});

However, like you see, I don't need to override all of the methods. Even worse, by overriding them I lose functionality. Is there a way keep the original methods and only override certain ones?

like image 844
Wouter Avatar asked Dec 13 '22 10:12

Wouter


1 Answers

You have no choice, you need to implement them all. However, a common pattern is to have a base class that provides no-op implementations of the methods in the interface, and then to extend that base class when you need to do something useful:

public class BaseSVGEventListener implements BaseSVGEventListener {
            public void keyPressed(int i) {}
            public void keyReleased(int i) {}
            public void pointerPressed(int i, int i1) {}
            public void pointerReleased(int i, int i1) {}
            public void hideNotify() {}
            public void showNotify() {}
            public void sizeChanged(int i, int i1) {}
}

And then:

svgForm.setSVGEventListener(new BaseSVGEventListener (){
            public void keyPressed(int i) {
                System.out.println("Val"+i);
            }
            public void keyReleased(int i) {
                System.out.println("Val"+i);
            }
});

There are many examples of this in the Java AWT/Swing APIs, for example FocusListener and FocusAdapter:

An abstract adapter class for receiving keyboard focus events. The methods in this class are empty. This class exists as convenience for creating listener objects.

Extend this class to create a FocusEvent listener and override the methods for the events of interest. (If you implement the FocusListener interface, you have to define all of the methods in it. This abstract class defines null methods for them all, so you can only have to define methods for events you care about.)

The Java API calls these "adapters", although that's a rather questionable use of that term. The "adapter pattern" is supposed to be used to adapt one type to another, which isn't what these API classes do.

like image 55
skaffman Avatar answered Jan 04 '23 16:01

skaffman