- Adapter class is a simple java class that implements an interface with only EMPTY implementation .
- Instead of implementing interface if we extends Adapter class ,we provide implementation only for require method
- An adapter class provides the default implementation of all methods in an EventListener interface. Adapter classes are very useful when you want to process only few of the events that are handled by a particular event listener interface.Here's a mouse adapter that beeps when the mouse is clicked
Without extending theimport java.awt.*; import java.awt.event.*; public class MouseBeeper extends MouseAdapter { public void mouseClicked(MouseEvent evt) { Toolkit.getDefaultToolkit().beep(); } }
MouseAdapter
class, I would have had to write the same class like this
Adapter classes are a minor convenience. You do not need to use the adapter classes if you don't want to.import java.awt.*; import java.awt.event.*; public class MouseBeeper implements MouseListener { public void mouseClicked(MouseEvent evt) { Toolkit.getDefaultToolkit().beep(); } public void mousePressed(MouseEvent evt) {} public void mouseReleased(MouseEvent evt) {} public void mouseEntered(MouseEvent evt) {} public void mouseExited(MouseEvent evt) {} }
Click here for full tutorial of Adapter Classes
PUB ID
Tuesday, 5 January 2016
What is Adapter class in Java?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment