Marker interface and Adapter classes

Marker interface and Adapter classes

                                             Marker Interface

If an interface does not contain any methods and by implementing that interface if our object will get some ability such type of interface is called a marker interface or ability interface or tag interface.

eg-:

  • Serializable(I)
  • cloneable (I)
  • RandomAccess(I)
  • SingleThreadModel(I)  
      these are methods of some marker interface or ability interface or tag interface.

eg-:
    by implementing a serializable interface, our object can be saved to the file and travel across the network.

eg-:
    by implementing a clonable interface out objects in a position to produce exactly duplicate cloned objects.


Ques-: without having any method how will the object get the ability in marker interfaces?

Ans-: integrally JVM is responsible to provide the required ability.

Ques-: why JVM is providing the required ability in marker interfaces ?

Ans -: to reduce the complexity of programming and to make Java language as simple.

Ques-:Is it possible to create our own marker interface?

Ans-:  yes but customization of JVM is required.


                                            Adapter classes

The adapter class is a simple Java class that implements an interface with only an empty implementation.

eg-:

interface interf{

      void m();
      void m1();
      void m2();
      void m3();
      void m4();
      void m5();
            .
            .
            .
            .
      void mn();
}
abstract class Adapterx implements interf{
      void m(){}
      void m1(){}
      void m2(){}
      void m3(){}
      void m4(){}
      void m5(){}
            .
            .
            .
            .
      void mn(){}
}


if we implement an interface for each and every method of that interface compulsory we should provide implementation whether it is required are not required.

eg-:

class Test implements interf{
      void m(){
           // 100 line of code.
          System.out.print("hello");
      }
      void m1(){}
      void m2(){}
      void m3(){}
      void m4(){}
      void m5(){}
            .
            .
            .
            .
      void mn(){}
}

  • the problem with this approach is it increases the length of the code and reduces its readability.
  • we can solve this problem by using adopter classes.
  • instead of implementing the interface if we extend the adapter class we have to provide implementation only for required methods and we are not responsible to provide implementation for each and every method of the interface so that length of the code will be reduced.

eg-:

class Test extends Adapterx{
    void m1(){

     }
}

class Test1 extends Adapterx{
    void m2(){

     }
}

class Test2 extends Adapterx{
    void m3(){

     }
}

class Test3 extends Adapterx{
    void m4(){

     }
}

We can develop a servlet in the following three ways.
  • by implementing a servlet interface-: if we implement a servlet interface for each and every method of that interface we should provide implementation it increases the length of code and reduces readability.
  • by extending the GenericServlet (AC)-: inserted of implementing servlet interface directly if we extend generic servlet we have to provide implementation only for service method and for all remaining methods not required to provide implementation .hence more are less generic servlet access adapter class for servlet interface.
    • by extending the HTTPServlet (AC).

    Note-: marker interface and adapter classes simplify the complexity of programming and these are the best utilities to the programmer and the programmer will become simple.