• Post Reply Bookmark Topic Watch Topic
  • New Topic

inherited vs. implements Listeners  RSS feed

 
Joseph Maddison
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working with a sample class that extends JFrame. I noticed that I had to add "implements KeyListener" and declare the appropriate methods as required. That's not too surprising, but then I added a method on the WindownListener interface to listen for the window close event. It struck me as odd that I didn't need to implement the other methods. A little digging and I see that this is because one of the ancestors of JFrame implements WindowsListener (I think it was Component). So far, so good.

Interestingly enough, if I add "implements WindowListener" to my class, I now have to provide an implementation for each of the methods required in the interface, even though those methods already exist higher in the tree and would therefore be inherited in my JFrame subclass. Why is that?

Thanks,
jdmaddison
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any class that implements an interface is required to implement every method in the interface. For convenience we have adapter classes for interfaces that have larger numbers of methods, such as the WindowListener, ComponentListener, MouseListener interfaces, which allow you to implement only the methods you want to use. The adapter class has all the interface methods stubbed in. See the General Information about Writing Event Listeners page in the java tutorial for more on this.

... I added a method on the WindowListener interface to listen for the window close event. It struck me as odd that I didn't need to implement the other methods ...
Does it work, ie, is the method called when you close the JFrame? If so then it sounds like you are using some code (beyond the sdk) that includes a WindowAdapter in the background.

If it doesn't work then there might be some subtlety to explore — you can add a method to a class

but it won't do much. How will you add a WindowListener to a JFrame using this method? When you add a WindowListener that listener must either implement WindowListener or extend WindowAdapter. If it doesn't do one of those it isn't a WindowListener. The presence of one or more interface methods in a class body doesn't (constitute a WindowListener or) qualify the class as a member (or, more correctly, as being of the type, or as being an implementer) of the interface (whose methods have been added). If the class declares that it implements WindowListener then it is a WindowListener.

If you try to add a WindowListener to a JFrame (who counts Window as an ancestor) the compiler will complain if it doesn't find all the methods in the class body of the listener. Unless, of course, you use an adapter

[ January 16, 2005: Message edited by: Craig Wood ]
 
Joseph Maddison
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Craig Wood:
...For convenience we have adapter classes for interfaces that have larger numbers of methods, such as the WindowListener...


That's it exactly. I wasn't correctly interpreting what I was looking at. Sorry for the confusion and thanks for all the info!

jdmaddison
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!