Last Updated on March 22, 2023 by Prepbytes
Java is an object-oriented programming language that allows developers to build applications that are extremely functional and efficient. One of the java’s most important features is its ability to handle events and actions in applications. Java uses listener interfaces to describe an event’s behavior when it occurs. Adapter class in java makes it easy to implement listener interfaces without having to implement all of the interface’s methods. This article discusses the adapter class in java in detail. Before we begin, let’s have a brief look at what is adapter class in java.
What is an Adapter Class in Java?
An adapter class in java is a class that provides a default implementation of all methods in a listener interface. It is used to make listener interface implementation easier by providing default methods that can be modified as per requirement. Adapter classes are found in the java.awt.event and javax.swing.event packages, which are used to handle events in java graphical user interfaces (GUI). With the help of the adapter class, developers can establish event listeners by implementing only the methods that are needed for their application. This decreases the amount of code needed to implement listener interfaces while also increasing code readability and maintainability.
Relationship Between Adapter Class in Java and Listener Interface
When we wish to use the majority of the methods given under the event listener interface, we implement event listeners in our code. But, if we implement any listener interface directly in a class, we must implement all of the methods within that interface, which increases the code size unnecessarily. This type of difficulty can be resolved with the help of an adapter class in java. When we only intend to use selected methods, an adapter class is the best solution to create an event listener.
To use an adapter class, we just need to create a subclass for it and override the required methods. To further understand this, consider the example of MouseListener interface. The primary function of this interface is to be informed whenever the state of the mouse changes. There are five methods available in this interface: mouse clicked, mouseExited, mouseEntered, mousePressed, and mouseReleased. When an event occurs, even if only one of these methods is required, all of them must be implemented which means that methods that are not required are left untouched. To avoid this wasteful implementation, an adapter class is implemented.
Types of Adapter Classes in Java
The adapter class in java can be found in three separate packages. The three packages are as follows:
java.awt. event | Adapter Class in Java
All of the classes required for event handling in java are contained in the package java.awt.event. Each event class includes a user interface via which you can create handlers for it. A java interface is solely composed of abstract methods. If you create a class that implements an interface, whether you utilize it or not, you must give an implementation for each of its methods (the standard practice is to provide an empty block if you don’t use it).
To make programming easier, the java.awt.event package offers classes that implement the interfaces and provide default implementations for their methods (usually doing nothing). In the Java API, an event-handling interface is referred to as a listener interface.
When the state of an object changes, an event occurs. For example, clicking a button, dragging a mouse, and so on. The java.awt.event package contains a number of event classes and Listener interfaces for handling events.
Adapter Class | Listener Interface |
---|---|
WindowAdapter | WindowListener |
KeyAdapter | KeyListener |
MouseAdapter | MouseListener |
MouseMotionAdapter | MouseMotionListener |
FocusAdapter | FocusListener |
ComponentAdapter | ComponentListener |
ContainerAdapter | ContainerListener |
HierarchyBoundsAdapter | HierarchyBoundsListener |
java.awt.dnd | Adapter Class in Java
The java.awt.dnd package, introduced in Java 1.2, consists of classes and interfaces that simplify data transfer via the drag-and-drop metaphor. This feature is based on the data transfer framework provided by the java.awt.datatransfer package. A DropTarget object is a proxy for an object that wants to receive drops, whereas a DragSource object is a proxy for an item that starts dragging. This package’s numerous event classes manage all inter-object communication throughout the drag operation.
Adapter Class | Listener Interface |
---|---|
DragSourceAdapter | DragSourceListener |
DragTargetAdapter | DragTargetListener |
java.swing. event | Adapter Class in Java
The javax.swing.event package adds Swing-specific event objects, listeners, and adapters to the java.awt.event package. Event types are specified by classes with names that end in "Event," and their fields and methods provide information about the occurrence. Event listeners are interfaces whose names end in "Listener."
The methods of these interfaces are used to notify interested objects when specific events occur. Listener interface implementations with names that end in "Adapter" are useful no-op implementations. Subclassing an adapter class is typically less difficult than creating the corresponding listener interface from scratch.
Adapter Class | Listener Interface |
---|---|
MouseInputAdapter | MouseInputListener |
InternalFrameAdapter | InternalFrameListener |
Difference Between the Adapter Class in Java and the Object Adapter.
There are two types of adapter class in java: Class Adapter and Object Adapter. The main difference between Class Adapter and Object Adapter is in the way they achieve the adaptation of interfaces.
Class Adapter:
In the Class Adapter pattern, the adapter class extends the adaptee class and implements the target interface. This way, the adapter inherits the functionality of the adaptee and can also use it to fulfill the target interface’s requirements.
java
public class Adapter extends Adaptee implements Target {
@Override
public void request() {
specificRequest();
}
}
In this example, the Adapter class extends the Adaptee class and implements the Target interface. The request() method of the Adapter class calls the specificRequest() method of the Adaptee class to fulfill the Target interface’s requirements.
Object Adapter:
In Object Adapter pattern, the adapter class contains an instance of the adaptee class and implements the target interface. This way, the adapter can use the adaptee’s functionality to fulfill the target interface’s requirements.
public class Adapter implements Target { private Adaptee adaptee; public Adapter(Adaptee adaptee) { this.adaptee = adaptee; } @Override public void request() { adaptee.specificRequest(); } }
In this example, the Adapter class contains an instance of the Adaptee class and implements the Target interface. The request() method of the Adapter class calls the specificRequest() method of the Adaptee class to fulfill the Target interface’s requirements.
So, the key difference between the two types of Adapter classes is that the Class Adapter uses inheritance to adapt the interfaces, whereas Object Adapter uses composition. In general, Object Adapter is preferred over Class Adapter because it is more flexible and allows for easier swapping of adaptee classes.
Advantages of Adapter Class in Java
Here are some advantages of adapter class in java:
- With the adapter class, even distinct or unrelated classes can work (function) together.
- With the help of adapter class in java, similar classes can be used in a variety of ways.
- It contributes to increasing the visibility of the classes.
- It makes working with disconnected classes easier.
- It greatly improves the reusability of the classes.
- Users get an option to use the pluggable kit to create apps, making it extremely handy.
- The adapter class also facilitates in grouping the patterns together in a class.
Conclusion
We’ve learned about the mechanics of a adapter class in java, the three primary packages that contain adapter classes, and the benefits of adapter classes to programmers. Adapter classes are abstract classes for receiving various events and exist as a convenience for creating listener objects. Adapter classes, with their various functionalities, provide programmers with a platform to be innovative using Swing methodology.
FAQs
Here are some frequently asked questions related to adapter class in java.
Q1: Is an adapter class an abstract class in Java?
Ans: An adapter class can be an abstract class or a concrete class, depending on the needs of the developer.
Q2: When should i use adapter class in java?
Ans: An adapter class in java should be used when a developer needs to implement an interface but only wants to override a subset of the interface’s methods.
Q3: What is the difference between an adapter class and a proxy class in Java?
Ans: An adapter class in java provides default implementations for all methods in an interface, while a proxy class provides a wrapper around an object to control its access.
Q4: Are there any disadvantages to using an adapter class in java?
Ans: One potential disadvantage of using an adapter class in java is that it can introduce additional complexity to the code, making it harder to understand.
Q5: Can an adapter class be used to add functionality to an existing class?
Ans: Yes, an adapter class can be used to add functionality to an existing class without modifying the existing class.
Q6: Can an adapter class in java be used to implement an interface without using inheritance?
Ans: Yes, an adapter class in java can be used to implement an interface without using inheritance, by creating a separate class that extends the adapter class and implements the interface.
Q7: Can an adapter class in java be used to override a method in an interface?
Ans: Yes, an adapter class in java can be used to override a method in an interface, allowing for more fine-grained control over the implementation of the interface.