Lets say I have this interface and class:
interface IListener
{
void f();
void g();
void h();
}
class Adapter implements IListener
{
void f() {}
void g() {}
void h() {}
}
What is the point of implementing thoose interface methods if they do nothing ?
Question taken from Design Patterns Java Workbook.
The aim is to makes the implementation of the interface easier if you don’t need all the methods. You can extend the adapter and override only the methods you want to. These classes only exist for convenience.
For example, in Swing, when implementing a
MouseListener, you might not want to treat all the mouse events. In this case, you can extendsMouseAdapterand treat only the events you are interested in.Please note that the
Adapterclass usually implements the interface as well.