Adapter Class In Java

Introduction

 
This article introduces the Adapter class in Java.
 

What is Adapter class

 
The Adapter class provides the default modification of all methods of an interface; we don't need to modify all the methods of the interface so we can say it reduces coding burden. Sometimes or often we need a few methods of an interface. For that, the Adapter class is very helpful since it already modifies all the methods of an interface and by implementing the Adapter class, we only need to modify the required methods.
 

Advantages of the Adapter class

  • Assists unrelated classes to work together.
  • Provides a way to use classes in multiple ways.
  • Increases the transparency of classes.
  • Its provides a way to include related patterns in a class.
  • It provides a pluggable kit for developing applications.
  • It makes a class highly reusable.
Examples
 
The following examples contain the following Adapter classes:
  • ContainerAdapter class.
  • KeyAdapter class.
  • FocusAdapter class.
  • WindowAdapter class.
  • MouseAdapter class.
  • ComponentAdapter class.
  • MouseMotionAdapter class.
  • etcetera
Let's use a scenario that shows the need for the Adapter class
 
As we know, the WindowListener interface contains seven methods. Whenever we need to use this interface we must modify or implement all 7 methods. So when we use the Adapter class (in other words WindowAdapter) we don't need to modify all the methods, since all the methods are already modified in it. We only need to implement those methods that are modified.
 

Methods of the WindowListener class

 
It contains the following public methods: 
 
The following method is used when the current Window is set to be activated.
  1. public void windowActivated(WindowEvent we)
The following method is used when a window is closed.
  1. public void windowClosed(WindowEvent we)  
When a user wants to close a window the following method is used.
  1. public void windowClosing(WindowEvent we)
The following method is used when the current window is not activated for a long time.
  1. public void windowDeactivated(WindowEvent we)  
The following method is used when a window changes its position from minimized to normal (restored) state.
  1. public void windowDeiconfied(WindowEvent we) 
The following method is used when a window is changed from normal to minimized state.
  1. public void windowIconified(WindowEvent we)  
The following method makes the window visible.
  1. public void windowIconified(WindowEvent we)  

Create an adapter class

 
Create an adapter using:
  1. public class WindowAdapter implements WindowListner  
  2. {  
  3. public void windowClosed(WindowEvent e){}  
  4. public void windowOpened(WindowEvent e){}  
  5. public void windowIconified(WindowEvent e){}  
  6. public void windowDeiconified(WindowEvent e){}  
  7. public void windowActivated(WindowEvent e){}  
  8. public void windowDeactivated(WindowEvent e){}  
  9. public void windowClosing(WindowEvent e){}  
  10. }   
The following is a test program using an Adapter class.
 
WindowAdapterEx.java
 
In this example; we are using a close method (in other words windowClosing) of the Adapter class that shows the benefits of using the Adapter class.
  1. import java.awt.event.WindowAdapter;  
  2. import java.awt.Frame;  
  3. import java.awt.event.WindowEvent;  
  4. public class WindowAdapterEx extends Frame  
  5. {  
  6.     public WindowAdapterEx()  
  7.     {  
  8.         WindowAdapterClose clsme = new WindowAdapterClose();  
  9.         addWindowListener(clsme);  
  10.         setTitle("WindowAdapter frame closing");  
  11.         setSize(400400);  
  12.         setVisible(true);  
  13.     }  
  14.     public static void main(String args[])  
  15.     {  
  16.         new WindowAdapterEx();  
  17.     }  
  18. }  
  19. class WindowAdapterClose extends WindowAdapter  
  20. {  
  21.     public void windowClosing(WindowEvent e)  
  22.     {  
  23.         System.exit(0);  
  24.     }  
  25. }   
Outputs
 
Adapter Class In Java
 
It generates the following frame:
 
Adapter Class In Java
 
Note: When we click on the "Close" link it is closed.


Similar Articles