The Adapter classes are an abstract class for receiving various events and the methods in these classes are empty.
These classes exist as a convenience for creating the listener objects.
- The Java provides a special feature called as an adapter class that can simplify the creation of the event handlers in certain situations.
- An adapter class provides empty implementation of all the methods in an event listener interface that is this class itself write the definition for methods which are already present in a particular event listener interface and However these definitions does not affect the program flow or meaning at all.
- The Adapter classes are very useful when we have to receive and process only some of the events that are already handled by a particular event listener interface.
- We can define a new class which can act as an event listener by extending one of the adapter classes & implementing only those events in which we are interested.
- Example- Suppose that we want to use the MouseClicked event or method from MouseListener and if we do not use adapter class then unnecessarily we have to define all the other methods from MouseListener as MouseReleased, MousePressed etc.
- But when we use adapter class then we can only define MouseClicked method and not to worry about other method definitions because adapter class provides an empty implementation of all the methods in an event listener interface.
- Below is the Table that Indicates Listener Interface with their respective adapter classes.
The Following is the list of some commonly used adapters while listening a GUI event in AWT.