Marker Interface

Marker Interface

In this tutorial, we are going to discuss marker interface in java. Marker interface in Java is interfaces with no field or methods or in simple word empty interface in java is called marker interface.

Marker Interface

E.g

Serializable, Cloneable, Remote interface

public interface Serializable {
  // nothing here
}

These interfaces are marked from some ability. It delivers the run-time type information about an object. It is the reason that the JVM and compiler have additional information about an object.

E.g

  1. By implementing a serializable interface, we can send objects across the network and save the state of an object to a file. This extra ability is provided through a Serializable interface.
  2. By implementing a cloneable interface, our object will be in a position to provide exactly duplicate objects.

Q. Marker interface won’t contain any method then how the objects will get that special ability?

Ans. JVM is responsible to provide required ability in marker interfaces

Q. Is it possible to create our own marker interface?

Ans. Yes. But customization of JVM is required.

Adapter class

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

interface X {
   m1();
   m2();
   m3();
}

abstract class Adapter Y implements X {
   m1() {}
   m2() {}
   m3() {}
}

If we implement an interface directly compulsory, we should provide an implementation for every method of that interface whether we are interested or not and whether it is required or not. It increases the length of the code. So that readability will be reduced.

class Test implements X {
   m1() { }
   m2() { }
   m3() { 
   ----
   ----
   }
}

class Test extends Adapter X {
   m3() {
   -----
   -----
   }
}

Suppose we extend the adapter class instead of the implementation interface directly. In that case, we have to provide an implementation only for the required method, but not all these approaches reduce the length of the code and improve readability.

Concrete class Vs abstract class Vs interface
  • We don’t know anything about implementation, and we just have requirement specifications. Then we should go for the interface.
  • We are talking about implementation but not completely (Just partially implementation), then we should go for abstract class.
  • We are talking about implementation completely and ready to provide service, and then we should go for concrete class.
Interface Vs Abstract Class
InterfaceAbstract Class
We don’t know anything about implementation just we have requirement specification. Then we should go for interface.We are talking about implementation but not completely (Just partially implementation) then we should go for abstract class.
Every method present inside interface is by default public and abstract.Every method present inside abstract class need not be public and abstract. We can take concrete methods also.
The following modifiers are not allowed for interface methods
strictfp, protected, static, native, private, final, synchronized
There are no restrictions for abstract class method modifier i.e., we can use any modifier
Every variable present inside interface is public, static, final by default whether we are declare or notAbstract class variables need not be public, final, static
For the interface variables we cannot declare the following modifiers private, protected, transient, volatileThere are no restrictions for abstract class variable modifier i.e., we can use any modifier
For the interface variables compulsory we should perform initialization at the time of declaration only.For the abstract class variables there is no restriction like performing initialization at the time of declaration
Inside interface we can’t take instance and static blocksInside abstract class we can take instance and static blocks
Inside interface we can’t take constructorInside abstract class we can take constructor

Q. Inside abstract class we can take constructor but we can’t create an object of abstract class, what is the need?

Abstract class constructor will be executed whenever we create a child class object to perform initialization of parent class instance variable at parent level only. This constructor is meant for child object creation only.

Q. Inside interface every method should be abstract where as in abstract class also we can take only abstract methods then what is the need of interface?

Interface purpose we can replace abstract class, but it is not a good programming practice we are miss using the role of an abstract class. We should bring abstract class into the picture whenever we are talking about implementation.

Marker Interface

Scroll to top