Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Adapter Class in Java is a very interesting topic that everyone must know about. In this article we will discuss the following topics:
Adapter classes provide an implementation of listener interfaces. When you inherit the adapter class implementation for all methods is not mandatory. Thus writing excess code is saved.
These adapter classes can be found in java.awt.event, java.awt.dnd and javax.swing.event packages. Some of the common adapter classes with corresponding listener interfaces are given below.
java.awt.event
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 | Listener Interface |
DragSourceAdapter | DragSourceListener |
DragTargetAdapter | DragTargetListener |
javax.swing.event
Adapter Class | Listener Interface |
MouseInputAdapter | MouseInputListener |
InternalFrameAdapter | InternalFrameListener |
import java.awt.*; import java.awt.event.*; public class MouseAdapterExample extends MouseAdapter{ Frame f; MouseAdapterExample(){ f=new Frame("Mouse Adapter"); f.addMouseListener(this); f.setSize(300,300); f.setLayout(null); f.setVisible(true); } public void mouseClicked(MouseEvent e) { Graphics g=f.getGraphics(); g.setColor(Color.BLUE); g.fillOval(e.getX(),e.getY(),30,30); } public static void main(String[] args) { new MouseAdapterExample(); } }
import java.awt.*; import java.awt.event.*; public class MouseMotionAdapterExample extends MouseMotionAdapter{ Frame f; MouseMotionAdapterExample(){ f=new Frame("Mouse Motion Adapter"); f.addMouseMotionListener(this); f.setSize(300,300); f.setLayout(null); f.setVisible(true); } public void mouseDragged(MouseEvent e) { Graphics g=f.getGraphics(); g.setColor(Color.ORANGE); g.fillOval(e.getX(),e.getY(),20,20); } public static void main(String[] args) { new MouseMotionAdapterExample(); } }
import java.awt.*; import java.awt.event.*; public class KeyAdapterExample extends KeyAdapter{ Label l; TextArea area; Frame f; KeyAdapterExample(){ f=new Frame("Key Adapter"); l=new Label(); l.setBounds(20,50,200,20); area=new TextArea(); area.setBounds(20,80,300, 300); area.addKeyListener(this); f.add(l);f.add(area); f.setSize(400,400); f.setLayout(null); f.setVisible(true); } public void keyReleased(KeyEvent e) { String text=area.getText(); String words[]=text.split("s"); l.setText("Words: "+words.length+" Characters:"+text.length()); } public static void main(String[] args) { new KeyAdapterExample(); } }
It assists unrelated classes to work together and provides a way to use classes in multiple ways. It can be able to increase the transparency of classes. An adapter class provides a way to include related patterns inside a class. Users are provided an option of a pluggable kit for developing applications. Thus, the usage of classes become highly reusable.
An adapter design pattern is a structural design pattern, which allows two different interfaces to work together. The adapter pattern is capable of making two incompatible interfaces compatible without changing their existing code. The corresponding interfaces may be incompatible, but inner functionalities should match the requirement.
The adapter pattern is frequently made to suit an existing class with others without modifying their source code. In addition, they use a single class to join functionalities of independent or incompatible interfaces. Another name for the adapter pattern is known as the wrapper, i.e it is an alternative name shared with the decorator design pattern.
The pattern also converts the incompatible interfaces of a class into different interfaces that are nothing but the target. This is what the clients require in the end. Adapter patterns also let classes work together else it would be almost incompatible for interfaces to function together. To bring things into perspective, consider a person traveling frequently to different countries with his laptop and mobile.
There are different electric sockets, voltages, and frequency measured in different countries and that makes the use of any appliance of one country to be compatible in a different country. In the UK, a Type G socket with 230 volts and 50 Hz frequency is commonly used.
In the US, a Type A and Type B sockets with 120 volts and 60 Hz frequency are practiced. In India Type C, Type D. and Type M sockets with 230 volts and 50 Hz is used. Lastly, in Japan, Type A and Type B sockets with 110 volts and 50 Hz frequency are used. Thus, it can be concluded that the appliances we carry may be incompatible with the electric specifications we have at different places. Similarly, adapter tools are essential because they can convert incompatible code into compatible code.
With this, we come to the end of this Adapter Class in Java. Check out the Java Online Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course are designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “Adapter Class in Java” blog and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 22nd February,2025 22nd February SAT&SUN (Weekend Batch) | View Details |
edureka.co