A marker interface in Java is an interface with no fields or methods. Put more simply, an empty interface in Java is called a marker interface. Examples of marker interfaces are the Serializable, Cloneable, and Remote interfaces. These are used to indicate some information to compilers or JVMs. So if the JVM sees that a class is Serializable, it can do some special operation on it.
Similarly, if the JVM sees some class is implementing Cloneable, it can perform some operations to support cloning. The same is true for RMI and the Remote interface. So in short, a marker interface indicates a signal or a command to the compiler or JVM.
The main purpose of marker interfaces is to create special types where types themselves have no behavior of their own.
} public boolean save(Object object) throws InvalidEntityFoundException { if(!(object instanceof MarkerEntity)) { throw new InvalidEntityFoundException("Invalid Entity Found, can't be saved); } return db.save(object); }
public interface MarkerEntity {
Here save method makes sure that only the objects of classes that implement the MarkerEntity interface are saved, for other types InvalidEntityFoundException is thrown. So here MarkerEntity marker interface is defining a type that adds special behavior to the classes implementing it.
Though annotations can also be used now to mark classes for some special treatments marker annotations are replacement for a naming pattern not for Marker interfaces.
But marker annotations can't fully replace the marker interfaces because; marker interfaces are used to define the type (as already explained above) whereas marker annotations do not.
Examples of Marker Interface which are used in real-time applications :
1). Cloneable interface: Cloneable interface is present in java.lang package. There is a method clone() in the Object class. A class that implements the Cloneable interface indicates that it is legal for the clone() method to make a field-for-field copy of instances of that class. Invoking Object’s clone method on an instance of the class that does not implement the Cloneable interface results in an exception CloneNotSupportedException being thrown. By convention, classes that implement this interface should override Object.clone() method.
2). Serializable interface: The serializable interface is present in the java.io package. It is used to make an object eligible for saving its state into a file. This is called Serialization. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable.
3). Remote interface: Remote interface is present in java.rmi package. A remote object is an object which is stored at one machine and accessed from another machine. So, to make an object a remote object, we need to flag it with the Remote interface. Here, the Remote interface serves to identify interfaces whose methods may be invoked from a non-local virtual machine. Any object that is a remote object must directly or indirectly implement this interface. RMI (Remote Method Invocation) provides some convenience classes that remote object implementations can extend which facilitate remote object creation.
Custom Marker Interface
we can create a marker interface. Here is an example that indicates whether an object can be saved to the database.
}
public interface Saved {
In order to save an entity to the database, the object representing this entity has to implement our Saved marker interface:
// implementation }
public class StudentEntity implements Saved {
we have a DAO object with a method for saving entities to the database. We can write our save() method so that only objects implementing our marker interface can be saved.
public boolean save(Object object) throws InvalidEntityFoundException { if(!(object instanceof Saved)) { throw new InvalidEntityFoundException("Invalid Entity Found, can't be saved); } return db.save(object); } }
public class StudentDAO {
In this article, we discussed what are marker interfaces in java and how they can be used.
0 Comments
Post a Comment