SEARCH BY

All

  • All
  • Java8
  • Spring

Marker interfaces in java

Post a Comment

Introduction

Marker interface is an interface which has no methods and no fields inside. Marker interfaces are also called as tagging interfaces

What is the use of marker interfaces

Marker interfaces are simply tells to the JVM that implementation classes are having a special behaviour during runtime. So implementing class no need to provide any implementation code for marker interfaces because of its empty body.

Some useful marker interfaces in java

There are bunch of interface are available in java, but here iam listing most popular marker interfaces.

How to create our own or custom marker interfaces

As the definition of marker interface first create an empty interface
public interface DeletableStudent {
// do not add any lines of code here
}
Now we need to create a class which implements this class, but no extra code can be added related to marker interface.
In this example iam trying to take a condition that to perform an action on the object of implementing class we need to check whether the class is implementing marker class or not.
public class Student implements DeletableStudent {
// add the body of Student class
}
The above class implements DeletavleStudent.java ( marker interface ) , for simplicity we are not adding the remaining code of Student.java
Next we are going to use this custom marker class usage in the following example
public class CrudClass {

 public boolean deleteStudent(Student s) {

  if (s instanceof DeletableStudent) {
   // add delete logic here
   
   return true;
  } else {

   return false;
  }
 }

}
In line number 5 in the above program we have used marker interface DeletableStudent, to test whether the Student object s is an instanceOf marker interface or not, that means DeletableInterface tells to the JVM with our logic , that perform delete logic if Student is instance of marker interface.

Annotations vs Marker interfaces

Like marker interfaces we can do same with annotations, except polymorphism will take place as an advantage while using marker interface. With using marker interfaces we can also use polymorphism concepts, but with annotations it is not possible.
Consider an example, where Account is the super class of SavingsAccount, CurrentAccount and TradingAccount classes. And Account is a serializable class then automatically three account classes will be serializable classes.
public class Account implements Serializable {
// body of Account class
}
In the above code Account.java is a class which extends Serializable marker interface, then by default all implementing class will be serializable too.
public class SavingsAccount extends Account {
 // implementation logic of Account class
 // body of SavingsAccont
}
public class CurrentAccount extends Account {
 //implementation logic of Account class
 //body of CurrentAccount
}
public class TradingAccount extends Account {
 // implementation logic of Account class
 // body of TradingAccount
}
jaya
I love software designing, coding, writing and teaching...

Share this article

Related Posts

Post a Comment

Place your code in <em> </em> tags to send in comments | Do not add hyper links in commnet

Close

Subscribe our newsletter for the latest articles directly into your email inbox