Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Adapter Class in Java: Practical Guide with Examples

Updated on 17 December, 2024

22.25K+ views
16 min read

Have you ever needed to charge your phone while you’re traveling in a car? What do you do? You use a car charger adapter that converts the 12V cigarette lighter socket into a USB port, allowing you to charge your phone effortlessly.

In Java, an adapter class works the same way—it bridges the gap between two incompatible systems, allowing them to work together seamlessly. Whether you’re dealing with legacy systems or integrating third-party libraries, the adapter ensures everything functions as intended, just like your car charger adapter!

Understanding this concept of bridging incompatible systems will definitely make you a standout in the tech world. It will open doors to high-demand roles in cybersecurity, data science, and software development. 

Ready to level up your programming career? Dive into this practical guide and start augmenting your knowledge and skills.

What is an Adapter Class in Java?

An adapter class in Java is a design pattern used to allow two incompatible interfaces to work together. It acts as a middleman that converts the interface of a class into another interface that the client expects. Instead of rewriting code or forcing classes to match, the adapter class simplifies integration and makes systems more flexible.

Adapter classes are often used in situations where you're dealing with abstract classes or interfaces that have multiple methods. By creating an adapter, you don't need to implement every method in the interface. Instead, you can extend an existing class and override only the methods you need. This makes the code cleaner and more efficient.

Here are a few key features of adapter class:

  • Implements interfaces or extends abstract classes with multiple methods
  • Provides default empty method implementations
  • Can be used to make legacy systems compatible with new ones
  • Simplifies handling of complex interfaces by only focusing on necessary methods

Also Read: Abstract Class vs Interface: The Differences and the Similarities | upGrad blog

Why Use Adapter Class?

When working with interfaces that contain multiple methods, you often end up implementing methods that aren't relevant to your task. An adapter class offers a cleaner solution by allowing you to implement only the methods you need, simplifying your code.

Common challenges with interfaces containing multiple methods:

  • Unnecessary code complexity.
  • Forced implementation of irrelevant methods.

How adapter classes simplify your code:

  • Makes your code more readable, maintainable, and less prone to errors.
  • You only implement the methods you actually need, cutting down on unnecessary code.

Real-world scenarios where adapter classes are helpful:

  • Adapting custom UI components to fit into a predefined framework.
  • Integrating legacy systems with modern applications without major rewrites.
  • Using third-party libraries with incompatible methods, without needing to modify the library code.

Adapter classes aren't just a convenience—they’re a powerful tool that helps you streamline development and work more efficiently.

Also Read: Java Tutorial: Learn Java Programming From Scratch For Beginners

Now that you know what is adapter class is, let's dive into some common types of adapter classes.

What are the Different Types of Adapter Classes in Java? 

An adapter class in Java is used in various libraries to simplify event handling. These classes act as intermediaries between events and listeners, making it easier to implement only the methods you need. 

Let’s explore some common types of adapter classes.

 java.awt.event adapter classes

In Java’s AWT (Abstract Window Toolkit), adapter classes help manage common events like mouse clicks, key presses, and window actions. These classes implement the corresponding listener interfaces and offer default empty implementations, so you only need to override the methods you need.

Examples:

  • MouseAdapter handles mouse events.
  • KeyAdapter handles keyboard events.

Now, let's see the table for these adapter classes and their associated listener interfaces:

Adapter Class

Listener Interface

MouseAdapter MouseListener
KeyAdapter KeyListener
WindowAdapter WindowListener
ComponentAdapter ComponentListener

Explanation: These adapter classes make it easier to handle events by providing default methods for each listener interface. You only need to override the specific methods that your program requires, which reduces code clutter and complexity.

java.awt.dnd adapter classes

Drag-and-drop events are managed by the java.awt.dnd package. The adapter classes here simplify the handling of these events, letting you focus on the relevant actions like dragging and dropping items.

Examples:

  • DragSourceAdapter for drag actions.
  • DropTargetAdapter for drop events.

Here’s a table showing these adapter classes and their corresponding listener interfaces:

Adapter Class

Listener Interface

DragSourceAdapter DragSourceListener
DropTargetAdapter DropTargetListener

Explanation: These adapter classes are specifically designed to handle drag-and-drop events. They provide default implementations of the DragSourceListener and DropTargetListener interfaces, so you only need to focus on the methods that are necessary for your application.

 javax.swing.event adapter classes

For Swing-based applications, adapter classes help manage events in GUI components. These classes make it easier to work with user interface events without dealing with every single method in the listener interfaces.

Examples:

  • MouseAdapter for handling mouse actions in Swing.
  • WindowAdapter for managing window events in Swing-based applications.

Let’s take a closer look at how these adapter classes are mapped to their listener interfaces:

Adapter Class

Listener Interface

MouseAdapter MouseListener
WindowAdapter WindowListener
KeyAdapter KeyListener

These adapter classes are key in reducing complexity, ensuring that you only implement the necessary event methods while maintaining a clean, readable codebase.

Also Read: Java Swing Project: Properties, Advantages & Frameworks | upGrad blog

Now that you know what is adapter class and its different types, let's go through the process step by step.

How to Create an Adapter Class in Java?

Creating your own adapter class in Java is a straightforward process. It allows you to bridge the gap between incompatible interfaces by overriding only the methods you need. Let’s walk through how to write a custom adapter class.

Here’s a step-by-step guide:

  1. Define the interface: First, you need an interface with multiple methods. This is the interface you want to adapt.
  2. Create the Adapter Class: Next, create a class that implements the interface. Instead of implementing every method, you can simply extend an existing adapter class and override only the relevant methods.
  3. Override the Necessary Methods: You’ll override only the methods that your program needs, leaving others as default (empty).
  4. Use the Adapter Class: Finally, use the custom adapter class in your program, passing it wherever the interface is required.

Example: Creating a Custom Adapter Class

Let’s say you have a custom interface called ShapeListener that defines methods for handling shape events like onDraw and onResize.

// Step 1: Define the Interface
interface ShapeListener {
    void onDraw();
    void onResize();
}

// Step 2: Create the Adapter Class
class ShapeAdapter implements ShapeListener {
    @Override
    public void onDraw() {
        // Default empty implementation
    }

    @Override
    public void onResize() {
        // Default empty implementation
    }
}

// Step 3: Use the Adapter Class
public class MyApp {
    public static void main(String[] args) {
        ShapeListener listener = new ShapeAdapter() {
            @Override
            public void onDraw() {
                System.out.println("Drawing a shape!");
            }
        };

        listener.onDraw();  // Output: Drawing a shape!
}
}

Explanation: In this example, ShapeAdapter implements the ShapeListener interface. Instead of implementing both methods, it provides default (empty) implementations for onDraw and onResize. In the MyApp class, you create an anonymous subclass of ShapeAdapter to override only the method you need—in this case, onDraw. This keeps the code clean and avoids unnecessary method implementations.

Also Read: 22 Best Java Project Ideas & Topics For Beginners With Source Code 2024 [Latest]

Adapter classes are particularly useful when you want to handle specific events without dealing with unnecessary methods. Let’s look at some examples to understand how these adapter classes simplify event handling.

How Are Adapter Classes Used in Java? Examples Explained

To see how adapter classes work in action, let's explore a few practical examples. Each of these will demonstrate how adapter classes help simplify event handling in Java.

Example 1: Using WindowAdapter Class

Overview of the Class: The WindowAdapter class provides default implementations for all the methods in WindowListener. This allows you to focus on the specific event you want to handle, such as window closing.

Example Code:

import java.awt.event.*;

public class WindowExample {
    public static void main(String[] args) {
Frame frame = new Frame("Window Example");

        // Using windowadapter to handle only windowClosing event
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("Window is closing...");
                System.exit(0);
            }
        });

        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}

Explanation: In this example, the WindowAdapter class is used to handle the windowClosing event. Instead of implementing all the methods in WindowListener, you override just the method you need. This makes the code simpler and cleaner.

Example 2: Using KeyAdapter Class

The KeyAdapter class simplifies handling keyboard events. It implements the KeyListener interface and provides default empty implementations for all the methods, allowing you to focus only on the key events you care about.

Overview of the class: KeyAdapter provides default implementations for keyPressed(), keyReleased(), and keyTyped(). You can override just the ones that are necessary for your application.

Example Code:

import java.awt.event.*;

public class KeyEventExample {
    public static void main(String[] args) {
Frame frame = new Frame("Key Event Example");

        // Using keyadapter to handle keyPressed event
        frame.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                System.out.println("Key pressed: " + e.getKeyChar());
            }
        });

        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}

Explanation: Here, the KeyAdapter class is used to listen for key presses. By overriding just keyPressed(), you avoid the need to implement unnecessary methods, keeping the code focused and efficient.

Example 3: Using MouseMotionAdapter Class

The MouseMotionAdapter class is used to handle mouse movement events, such as when the mouse is moved or dragged across the screen. Like the other adapter classes, it provides default implementations for the MouseMotionListener interface.

Overview of the class: MouseMotionAdapter simplifies the handling of mouse movement events by providing empty implementations of methods like mouseMoved() and mouseDragged(). You only need to override the methods that are relevant.

Example Code:

import java.awt.event.*;

public class MouseMotionExample {
    public static void main(String[] args) {
        Frame frame = new Frame("Mouse Motion Example");// Using mousemotionadapter to handle mouseMoved event
        frame.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                System.out.println("Mouse moved to: (" + e.getX() + ", " + e.getY() + ")");
            }
        });

        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}

Explanation: In this example, you use MouseMotionAdapter to track mouse movements. By overriding just the mouseMoved() method, you keep the code concise and relevant, without implementing the entire MouseMotionListener interface.

These examples highlight how adapter classes help reduce boilerplate code, making event handling in Java easier and more manageable.

Also Read: Top 8 Reasons Why Java Is So Popular and Widely Used in 2025

Adapter classes and object adapters both serve to bridge incompatible interfaces, but they do so in different ways. Let’s dive into the key differences.

What is the Difference Between Adapter Class and Object Adapter?

Both the adapter class and object adapter are design patterns used to make incompatible interfaces work together. However, they have key differences in how they function.

Here’s a table differentiating the two:

Aspect

Adapter Class

Object Adapter

Definition Uses inheritance to adapt an interface. It extends the class and overrides the necessary methods. Uses composition to adapt an interface by including an instance of another class.
How it works The adapter class extends the class to implement the required methods. The adapter class holds a reference to the target class and delegates method calls.
Key Feature Inherits behavior from a class and implements the target interface. Composes behavior by holding an object and delegating method calls.
Advantages Simple when adapting interfaces of a single class. More flexible as it allows adapting multiple classes and avoiding inheritance issues.
Disadvantages Tied to the class it extends, limiting flexibility. Can be slightly more complex due to composition and delegation.

Example Code: Using Adapter Class in Java

In this example, we’ll use an adapter class to implement a Shape interface that has multiple methods. We’ll create an adapter class for Rectangle that only overrides the methods you need.

Step 1: Define the Shape interface

interface Shape {
    void draw();  // method to draw the shape
    void resize();  // method to resize the shape
    void color();  // method to color the shape
}

Step 2: Create an adapter class that implements Shape

// Adapter class that implements Shape but provides default behavior for unused methods
class ShapeAdapter implements Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a rectangle.");
    }

    @Override
    public void resize() {
        // Default implementation can be left empty
    }

    @Override
    public void color() {
        // Default implementation can be left empty
    }
}

Step 3: Use the adapter class in your program

public class AdapterExample {
    public static void main(String[] args) {
        Shape rectangle = new ShapeAdapter();
        rectangle.draw();  // Output: Drawing a rectangle.
}
}

Explanation:

  • The ShapeAdapter class implements the Shape interface, but it only overrides the draw method, as it's the only one you need.
  • The resize and color methods have default (empty) implementations because they aren't required in this scenario.
  • This allows you to simplify the code and handle only the relevant methods.

This is a basic demonstration of how an adapter class simplifies interface implementation by overriding only the methods that are needed, making your code cleaner and more focused.

Example Code: Using Object Adapter in Java

Let’s assume you need to adapt a MediaPlayer interface to work with an existing AudioPlayer class that has its own method for playing audio.

Step 1: Define the MediaPlayer interface

interface MediaPlayer {
    void play();
}

Step 2: Define the AudioPlayer class with its own method

class AudioPlayer {
    public void playAudio() {
        System.out.println("Playing audio...");
    }
}

Step 3: Create the Object Adapter that adapts the AudioPlayer class to the MediaPlayer interface

// Object Adapter
class MediaAdapter implements MediaPlayer {
    private AudioPlayer audioPlayer;

    public MediaAdapter(AudioPlayer audioPlayer) {
        this.audioPlayer = audioPlayer;
    }

    @Override
    public void play() {
        audioPlayer.playAudio();  // Adapting the playAudio method to play method
    }
}

Step 4: Use the adapter class in your program

public class AdapterExample {
    public static void main(String[] args) {
        AudioPlayer audioPlayer = new AudioPlayer();
        MediaPlayer mediaPlayer = new MediaAdapter(audioPlayer);  // Pass the AudioPlayer to the adapter
        mediaPlayer.play();  // Output: Playing audio...
    }
}

Explanation:

  • In this example, the MediaAdapter class does not extend the AudioPlayer class. Instead, it contains an instance of AudioPlayer and delegates the play method call to audioPlayer.playAudio().
  • This is a typical example of object composition where the adapter class adapts one interface (MediaPlayer) to another (AudioPlayer) without using inheritance.

This approach gives you more flexibility, as the adapter can delegate calls to different classes based on the context.

Both the adapter class and object adapter patterns serve to bridge incompatible interfaces, but they do so in different ways. While class adapters use inheritance to extend a class, object adapters rely on composition, making them more flexible and reusable across different scenarios. 

Also Read: Design Patterns: Singleton in Java

Understanding these distinctions is crucial for choosing between an adapter class and a listener interface, depending on your use case complexity and control needs. 

upGrad’s Exclusive Software and Tech Webinar for you –

SAAS Business – What is So Different?

 

 

What is the Difference Between an Adapter Class in Java and a Listener Interface?

Both the adapter class and listener interface are used to handle events in Java, but they differ in their purpose, complexity, and how they are implemented. Understanding the key differences can help you choose the right approach for your specific needs.

Here’s a table differentiating the approaches:

Factors

Adapter Class

Listener Interface

Purpose Simplifies event handling by providing default method implementations, allowing you to override only the ones you need. Defines the contract for handling events; all methods must be implemented.
Ease of Implementation Easier to implement when you only need to handle some methods, as it provides default implementations. Requires you to implement every method in the interface, even if some aren't needed.
Code Complexity Less complex, as you only override the necessary methods. More complex, as you must implement all methods, even if they aren't used.
Use Case Ideal when you need to handle specific events in a large interface without cluttering your code. Suitable for cases where you need full control over all events in an interface.
Inheritance Involves inheritance, as the adapter class typically extends an existing class. Does not involve inheritance; it relies on implementing the interface.
Extensibility Less flexible since the adapter class is constrained by its inheritance. More flexible, allowing you to implement the interface in various ways.
Availability Available in Java AWT and Swing libraries as predefined adapter classes. Must be created and implemented manually for custom event handling.
Example (Brief) MouseAdapter extends MouseListener, overriding only the methods needed. MouseListener interface requires the implementation of methods like mousePressed, mouseReleased, etc.

Explanation: An adapter class is perfect when you want to handle only a few methods of a larger interface without having to implement the entire interface. It reduces code complexity and makes event handling easier. 

On the other hand, a listener interface gives you full control over all methods but requires more effort and leads to more code. Both approaches have their uses, depending on whether you need simplicity or full customization.

Also Read: Ultimate Guide to Synchronization in Java

However, to understand when and why to use an adapter class, it's important to weigh its advantages and limitations.

What are the Pros and Cons of an Adapter Class in Java? 

The adapter class pattern simplifies event handling by providing default method implementations, allowing you to override only the ones you need. However, like any design pattern, it comes with both benefits and limitations.

Here is a list of pros:

  • Enhanced code readability: By reducing the need to implement unused methods, the code becomes cleaner and easier to read.
  • Reduced complexity in event handling: With default method implementations, you don’t have to implement every method, making event handling more manageable.
  • Flexibility for developers: The adapter class offers flexibility by allowing you to handle only the relevant methods for your use case, while ignoring unnecessary ones.

Here is a list of cons:

  • Lack of support for multiple interface implementations: The adapter class can only extend one class, limiting its ability to implement multiple interfaces.
  • Increased inheritance depth: In cases where multiple adapter classes are used, inheritance depth can increase, leading to more complex hierarchies.

The adapter class is useful when you want to streamline event handling or interface integration, but it may not be the best option in situations requiring flexibility across multiple interfaces.

As you explore the power of adapter classes and dive deeper into Java programming, the next step is to ensure you're equipped with the right skills to thrive in the industry.

How Can upGrad Help You Build a Career in Programming?

To succeed in the tech world, continuous learning is key. With upGrad, you gain access to one of the leading online learning platforms, trusted by over 10 million learners globally. Choose from 200+ industry-relevant courses and connect with over 1,400 hiring partners to help you land your ideal job. 

Whether you're looking to sharpen your coding skills or dive deeper into specialized fields, upGrad offers the right programming courses to help you achieve your career goals. If you're an aspiring tech professional in India, these courses will set you on the path to success.

Here’s a quick look at some of the relevant programming courses offered by upGrad:

Course Title

Description

Java Object-oriented Programming Master the fundamentals of Object-Oriented Programming (OOP) in Java with this free course, and learn key concepts like classes, inheritance, and polymorphism.
JavaScript Basics from Scratch This free course offers a comprehensive introduction to fundamental programming concepts and web development skills using JavaScript.
Master of Design in User Experience Earn a Master’s in User Experience Design from Jindal School of Art and Architecture, and gain expertise in creating intuitive, user-centered designs for digital products.
Best Full Stack Developer Bootcamp 2024 A program designed to equip learners with essential skills in both front-end and back-end development, preparing them for successful careers in software engineering.
Python Programming Free courses | Basic Python Free Course This free course offers 12 hours of learning to develop problem-solving skills through coding exercises on lists, strings, and data structures like tuples, sets, and dictionaries.

 

Need help choosing what’s right for your career goals? upGrad offers free one-on-one expert career counseling sessions to help you define your goals, match your skills with the right opportunities, and create a roadmap for long-term success.

 

Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.

Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.

Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.

Build your foundation in software development with our Free Courses – practical, beginner-friendly, and 100% free!

Frequently Asked Questions (FAQs)

1. How is an adapter class related to the decorator pattern?

Both patterns allow you to modify or extend functionality without altering the original object. However, an adapter class focuses on making incompatible interfaces work together, while a decorator adds new behaviors to an existing object.

2. Can an adapter class be used for class or object inheritance?

An adapter class can be used for both, but it is typically used in cases where you are working with an interface and want to simplify its implementation.

3. What are some real-world use cases for the adapter class?

Adapter classes are often used in integrating legacy systems with newer technologies, connecting third-party libraries into existing code, and simplifying event handling in graphical user interfaces (GUIs).

4. Is an adapter class the same as an abstract class in Java?

No, while both provide default behavior, an adapter class specifically simplifies the implementation of interfaces by providing empty or default methods, whereas an abstract class can contain abstract and concrete methods to define partial behavior.

5. When should I choose an adapter class over a listener interface?

If you need to handle only some methods of an interface and don't want to deal with unnecessary methods, an adapter class is more efficient. A listener interface is better when you need to implement every method in an interface.

6. Can I use multiple adapter classes in a single project?

Yes, you can use multiple adapter classes in a project, especially when working with different interfaces or event models. This helps to avoid repetitive code and simplifies event handling.

7. What is the difference between an adapter class and a proxy class?

An adapter class changes the interface of an object to make it compatible with other interfaces, while a proxy class controls access to another object, often to add extra behavior like logging, caching, or security checks.

8. Are adapter classes part of any design patterns?

Yes, the adapter class is part of the Adapter Design Pattern, which is one of the structural patterns in object-oriented design. It allows objects with incompatible interfaces to work together.

9. How does the adapter pattern improve code maintainability?

By using an adapter pattern, you can isolate changes in interfaces from the rest of the code, making it easier to maintain and modify without affecting other components.

10. What is the difference between a class adapter and an object adapter?

A class adapter uses inheritance (extends a class) to adapt an interface, while an object adapter uses composition (delegates calls to an existing object).

11. Can an adapter class implement multiple interfaces?

An adapter class can implement multiple interfaces, but it can only extend one class. To handle multiple interfaces, you may need to create a custom adapter class or use object composition.

RELATED PROGRAMS