What use of adpterclass?

Showing Answers 1 - 5 of 5 Answers

While we are using Listener interface, we have to write the method for all the methods either we used or not used those methods,
                If we have not used those methods, then, we have to define those methods with empty blocks,

            

               JAVA provides a beautiful concept i.e., Adapter class.. in this, we define the method which we want to perform the requirement, no need to write the code for all the methods having in that adapter class... i.e,, adapter class overrides all the methods automatically which we have not written the code.. and implement the methods which we have written the code..


               For example, take WindowListener and WindowAdapter,... first one is Interface and Second one is Class.. when we are Using WindowListener, we must have write even the empty blocks whether we have no work with that methods like
                  windowActivated(WindowEvent we) { }
                  windowClosing(WindowEvent we) { }
                  windowClosed(WindowEvent we) { }
                  windowDeactivated(WindowEvent we) { }
                  windowIconifid(WindowEvent we) { }
                  windowDeiconified(WindowEvent we) { }
                  windowOpened(WindowEvent we) { }
   so, all these methods must defined by us when we are using that WindowListener Interface.... so, adapter class provides the user an advantage, that suppose, in our program, only one method is required, so, we need NOT have to write the code for all seven methods, and need NOT specify or define them... 
  
          THIS IS THE USE OF ADAPTER CLASS...
       examples ....          MouseAdapter               KeyAdapter                                                        MouseMotionAdapter    FocusAdapter    
ContainerAdapter         ComponentAdapter          etc.....................    

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.