Introduction
Marker interface is an interface which has no methods and no fields inside. Marker interfaces are also called as tagging interfacesWhat 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.- java.io.Serializable interface ( read more about serialization here )
- java.lang.Cloneable interface
- java.rmi.Remote interface
- ThreadSafe interface
How to create our own or custom marker interfaces
As the definition of marker interface first create an empty interfacepublic 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 }
Post a Comment
Post a Comment