Skip to main content

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.

    Popular posts from this blog

    Java

    Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

    Short Circuite Operators part 4

                                                                 Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

    Final Variable

    Final Instance Variable If the value of a variable changes from object to object, such a variable is called an instance variable . For every object, a separate copy of the instance variable will be created. Instance variables do not require explicit initialization; JVM always provides default values. Example: class Test { int x; // instance variable public static void main(String... args) { Test t = new Test(); System.out.println(t.x); // output: 0 } } If the instance variable is declared as final , then explicit initialization is mandatory. JVM will NOT provide default values. Example: class Test { final int x; } Compile-time Error: variable x might not have been initialized. Rule: A final instance variable must be initialized before constructor completion . Possible places for initialization: 1. At the time of declaration class Test { final int x = 10; } 2. Inside an instance blo...