The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
August 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

RE:Event Listener Interfaces and adapters classes

Posted by Kishori Sharan on August 24, 2000 at 9:10 PM

Hi Inder
Suppose you have an interface as follows.
interface InderInterface {
void m1 ( ) ;
int m2 ( ) ;
}

Now if you implement this interface in any class then you will have to implement the two methods m1 and m2 too like

class c1 implements InderInterface {
void m1 ( ) {

// You have to supply the body for m1 , event if it may emmpty
}

int m2 ( ) {

// You have to supply the body for m2 , event if it may emmpty
}

Now think of a situation in which a class wants to implement only one method m2() of InderInterface. In such a situation that class has to implement InderInterface and supply the implementations for both m1 () nad m2 () methods whereas the body of m1() method will be empty because this class was interested in implementing only m2() method. So this class will look like as follows.
class c1 implements InderInterface {
void m1 () { } // You are forece to write this line
int m2 ( ) {
// actual code goes here
}
}
In above case we wrote one line void m1 ( ) { }. What if InderInterface has 10 methods but class c2 is interested in implementing only m2 () method. In such a case class c2 will have to have 9 redundant methods implementations ( like m1 () in our case ) with empty bodies. How to get around this problem. Now let us define a class InderAdapter as follows.
class InderAdapter implements InderInterface {
void m1 () {
// only empty body
}
int m2 ( ) {
// only empty body

}

}

After having definition of InderAdapter we can rewrite the class c2 as follows.
class c1 extends InderAdapter {
int m2 ( ) {
// your actual code goes here
}
}
Now look, your class c2 implements InderInterface ( though indirectly ) and it is not forced to implement the method m1 () which was forced in earlier case when we tried to implement InderInterface to c2 directly.
The same applies in case of java event listener interfaces and event adapter classes. When you are interested in all the events of a particular type generated by the component then it hardly makes any difference if you use interface or adapter. However, if you are interested only in a few events of a particular type then it is always convenient to use adapter to save coding time and keep your code less as we did in case of c2 ( when we rewrote it ) .
Java Example: MouseListener is an interface which declares 5 methods:
1. void mouseClicked(MouseEvent e)
2. void mouseEntered(MouseEvent e)
3. void mouseExited(MouseEvent e)
4. void mousePressed(MouseEvent e)
5. void mouseReleased(MouseEvent e)
So if you are interested in all the five mouse events then you can implement MouseListener interface or MouseAdapter class. However, if you are interested only in mouseClicked then you should extend MouseAdapter class. However, no one stops you implementing MouseListener interface in this case either.
But, when your interface contains only one method then in either case you have to implement that method whether you just implement your interface or create an adapter class and inherit your class from that adapter. So, whenever, in java, there is only one method defined in an event listener then you won't have any corresponding adapter class. Good example is ActionListener interface which has no corresponding ActionAdapter class because it contains only one method actionPerformed ( ActionEvent e ) ;
However, if in some cases your event listener class has to extend some other class then you have no choice but to implement the listener interface because you cannot inherit your class from adapter as well as from your given class.
I think now you are clear about the differences between event listener interfaces and event adapter classes. Aren't you?

Thanx
Kishori




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us