Open In App

What is Java Adapter Class?

Improve
Improve
Like Article
Like
Save
Share
Report

Adapter Class is a simple java class that implements an interface with only an empty implementation. Let’s suppose, there is an interface Intref which has various methods as follows:

interface Intref{ 
    public void m1();    
    public void m2();    
    public void m3();    
    :    
    :    
    :    
    :    
    public void m1000(); 
}

As, in this interface, there are 1000 methods present and if we want to implement this interface in any particular class then we will have to override all of these 1000 methods of Intref in the implementation class. We can do this as follows:

class Test implements Intref{ 
    public void m1(){     
         // some statements    
    }    
    public void m2(){     
         // some statements    
    }    
    public void m3(){     
         // some statements    
    }    
    :    
    :    
    :    
    :    
    public void m1000(){     
         // some statements    
    } 
}  

The problem with this approach is that it increases the length of the code and reduces readability. We can solve this problem by using the Adapter class. Instead of implementing the interface if we extends the Adapter class to our Test class then we will have to provide the implementation only for the required methods that we will needed at the time of implementation and we are not responsible to provide the implementation for each and every method of the interface, so that the length of the code will be reduced. So, firstly we have to create a class Adapter and we have to implement the Intref interface and only provide the empty implementation for each and every method of the Intref interface.

And we will have to use an abstract modifier while creating an Adapter class because we are providing an empty implementation to the methods of an interface and we know that if the implementation of the method is incomplete then we are restricted to create an object of that class. Thus for restricting the creation of an object of the Adapter class we should have to use an abstract modifier with the Adapter class.

abstract class Adapter implements Intref{ 
    public void m1(){};   
    public void m2(){};   
    public void m3(){};   
    :    
    :    
    :    
    :    
    public void m1000(){};
}

Now we have to extends this Adapter class in our Test class and just override those required methods which we will require in the Test class.

class Test extends Adapter{ 
    public void m1(){     
         System.out.println("This is m1() method.");    
    }        
    public void m80{     
         System.out.println("This is m80() method.");    
    } 
}

Example 1:

Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
interface Intref{
    public void m1();
      public void m2();
      public void m3();
      public void m4();
      public void m5();
      public void m6();
      public void m7();
      public void m8();
      public void m9();
      public void m10();
}
 
abstract class Adapter implements Intref{
      // providing the empty implementation
    public void m1(){};
      public void m2(){};
    public void m3(){};
    public void m4(){};
    public void m5(){};
    public void m6(){};
    public void m7(){};
    public void m8(){};
    public void m9(){};
    public void m10(){};
}
 
class GFG extends Adapter{
      @Override
      public void m1(){
        System.out.println("This is m1() method.");
    }
      @Override
      public void m7(){
        System.out.println("This is m7() method.");
    }
    public static void main (String[] args) {
        GFG g = new GFG();
          g.m1();
          g.m7();
    }
}


Output

This is m1() method.
This is m7() method.

Let’s now use this Adapter class approach in our next example, we will consider that there is an interface Intref and in that interface, there are two methods addNumWith5 and multiplyNumWith5. 

Now we will have to implement this interface to our Adapter class and provide the empty implementation for the addNumWith5 method and also to multiplyNumWith5. After the empty implementation, we will have to extends this Adapter class in our GFG class, and as we all know that when we would use Interface and wants to override its methods then we will have to provide the implementation for all the methods which were presents in the interface. But here, in this example, we only override that method which we will require at that time or we can say that we only override that method which we would want to use.

Example 2:

Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
interface Intref {
    public void addNumWith5(int num);
    public void multiplyNumWith5(int num);
}
 
abstract class Adapter implements Intref {
    public void addNumWith5(int num) {}
    public void multiplyNumWith5(int num) {}
}
 
class GFG extends Adapter {
    @Override public void addNumWith5(int num)
    {
        // adding 5 with num
        int result = num + 5;
        System.out.println(
            "After adding 5 with num, the required number is : "
            + result);
    }
    public static void main(String[] args)
    {
        GFG obj = new GFG();
        obj.addNumWith5(10);
    }
}


Output

After adding 5 with num, the required number is : 15


Last Updated : 03 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads