Actually marker interface is nothing but it is a design pattern in computer science by using any languages we can implement and that provide run-time type information about objects for specific behavior. It provides run-time metadata to associate class and its actual implementation may be different from whatever developer implement, it adds some other thing at run time it is similar to generating proxies around classes at runtime for providing some extra processing. In java, it is used as interfaces with no method specified.
As in a normal interface specifies functionality which an implementing class must implement. So that in the normal interface developer decide the behavior of implementing class by provide interface method behavior but a marker interface does not follow that pattern. On the other side, the implementing class defines the behavior.
Popular Spring Tutorials
- Spring Tutorial
- Spring MVC Web Tutorial
- Spring Boot Tutorial
- Spring JDBC Tutorial
- Spring AOP Tutorial
- Spring Security Tutorial
Usage of Marker Interface in java
There are lots of marker interfaces available in java. We have the following major marker interfaces as under:
- Searilizable interface
- Cloneable interface
- Remote interface
- ThreadSafe interface
Marker interfaces in Java are used to indicate something specific to compiler or JVM that the class implementing any of these would have some special behavior. Suppose we have implementing Serializable interface then JVM sees a Class is implementing the Serializable interface it does some special operation on it and writes the state of the object into object stream. This object stream is then available to be read by another JVM.
Marker interface apart from it implementing classes and special behavior of its implementing classes, it provide logically segregation of codes for some particular functionality on the classes, it is very useful in the framework like spring, struts, etc. Marker interface also helps code coverage or code review tool to find bugs based on specified behavior of marker interfaces.
Searilizable interface in Java
A good example of use of marker interface in java is Serializable interface. A class implements this interface to indicate that its non-transient data members can be written to a byte steam or file system. For more details>>>.
Clonable interface in Java
Whenever any class implementing cloneable marker interface, it allows us to create the clone of an object of implementing. For getting clone of object we have to do type cast by its appropriate type. If a class is not implementing the cloneable interface, and we try to clone that object we get a CloneNotSupportedException . In the process of cloning, the constructor is not called rather an exact copy of the said object is created. But the object of which the clone is created, must implement the cloneable interface. For more details>>>.