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?
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:
And then:
There are many examples of this in the Java AWT/Swing APIs, for example
FocusListenerandFocusAdapter: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.