This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thanks Bodie !! Here's my question again ..  RSS feed

 
Meghna ks
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the possibilities that you may have to write your own adapter classes ?!! And why do we have to do this ?!!
Thanks
Meghna
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meghna,
The reason that adapter classes are used are convenience. Some of the event listener interfaces ( like WindowListener for example. ) have lots of methods in them. If you are only interested in one method in the interface, you still must code all the methods in the interface you are implementing (i.e. lots of blank methods), when anonymous inner classes were added, it was found to be easier on the programmer to have adapters - classes that implement the interface with all empty methods. Then you can just create an anonymous inner class that extends the adapter and just code the methods you are interested in.
(Aside - One pitfall of using adapters versus interfaces is that if you mess up the method signature you are using for event listening, the adapter will not give any warning... you are simply adding a new method to the class. However, if you are using an interface it will give you a warning... an error will be thrown at compile time stating that your class should be defined as anonymous because it does not implement method x of the interface.)
In answer to the question of writing your own... well, you obviously don't have to write event listener adapters ( unless you create your own events and event listeners ) since Java provides adapters for all the event listeners with more than one method. However, Adapter is not just a type of class... it is a pattern. Basically, the adapter pattern is a class that adapts ( i.e. changes ) one class to behave like another. So, the event listener adapters adapt the event listeners so they behave like classes, not like interfaces. Adapters can be used for many other purposes in addition to the event listener example you asked about. They are used alot in java.io (though they are not specifically called adapters) where you can make one input stream behave like a a different type of input stream. For more info on the Adapter Pattern ask in the OO forum here on JavaRanch.
HTH,
-Nate
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adapter classes are really just shortcuts for implementing interfaces. When you implement an interface, you are required to implement every method that the interface defines. Sometimes that is overkill, for example, in the case of a WindowListener. If you really just want to handle the windowClosing event, you will still have to implement all of the other methods defined by this interface in order to implement it.
A shortcut is to use an adapter class. You can extend it or use it as an anonymous inner class. That way you only have to define the method you are interested in, because the adapter already contains empty implementations of all of the methods in the interface it implements.
So, unless you are writing a custom interface, the chances of you needing to create your own adapter class is pretty low.
What are your thoughts on this, gang?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!