Subscribe to Get Free Material Updates!
Visit my new blog WebData Scraping - Web Scraping Service provider company in India.

Difference Between Adapter class and Listeners Interface



When we implement a listener interface in any class then we must have to implement all the methods declared in that interface because all the methods in an interface are final and must be override in class which implement it. For example consider the following program which demonstrates handling of mouse events by implementing listener interface.

MouseEvents.java 
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/*<applet code="MouseEvents" height="300" width="300">
</applet>*/

Public class MouseEvents extends Applet implements MouseListener
{
 Public void init()
 {
  addMouseListener(this);
 }
 Public void mouseClicked(MouseEvent me)
 {
  setBackground(Color.blue);
  repaint();
 }
 Public void mouseEntered(MouseEvent me)
 {
  setBackground(Color.green);
  repaint();
 }

 Public void mouseExited(MouseEvent me)
 {
  setBackground(Color.red);
  repaint();
 }

 Public void mousePressed(MouseEvent me)
 {
  setBackground(Color.white);
  repaint();
 }

 Public void mouseReleased(MouseEvent me)
 {
  setBackground(Color.yellow);
  repaint();
 }
}


Our above example for handling mouse events implements MouseListener interface so the class MouseEvent has to implement all the five methods listed below.

  1. public void mouseClicked(MouseEvent me)
  2. public void mouseEntered(MouseEvent me)
  3. public void mouseExited(MouseEvent me)
  4. public void mousePressed(MouseEvent me)
  5. public void mouseReleased(MouseEvent me)
This can be inconvenient because if we want to use only one or two methods in your program then? It is not suitable solution to implement all the methods every time even we don’t need them. Adapter class makes it easy to deal with this situation. An adapter class provides empty implementations of all methods defined by that interface.

Adapter classs are very useful if you want to override only some of the methods defined by that interface. Here the names of Listener interface and corresponding interface are given which are in java.awt.event package.


Now consider a situation in which we want to perform any action only when mouse clicked and mouse releases then we should have to override mouseClicked() and mouseReleased() method. In this case if we use the listener interface then we must have to implements all the above five methods, the adapter class MouseAdapter will minimize programmer’s work. Following example demonstrate the use of Adapter class in place of Listener interface.

MouseEvents.java
implementing MouseListener.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/*<applet code="MouseEvents" height="300" width="300">
</applet>*/

Public class MouseEvents extends Applet extens MouseAdapter
{


 Public void init()
 {
  addMouseListener(this);
 }

 /*no need to implement all the methods because default implementation given in adapter class*/
 Public void mouseClicked(MouseEvent me)
 {
  setBackground(Color.blue);
  repaint();
 }

 Public void mouseReleased(MouseEvent me)
 {
  setBackground(Color.yellow);
  repaint();
 }
}

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

 
Design by Wordpress Theme | Bloggerized by Free Blogger Templates | coupon codes