why we use marker interface in java

It indicates that the class (and consequently all the fields which aren't transient) are candidates for serialisation. And if you're building a framework reliant on serialisation, you can of course write a method thus:
to limit the classes you're prepared to accept. Because a serialized object needs to retain compatibility across systems, serialisation is an explicit design decision and hence requires the use of the marker interface, to identify such candidates. There's also a security aspect. You don't want to make everything serialisable - otherwise you can accidentally expose (say) passwords or other sensitive data via serialisation.

There are no marker interfaces concepts in JAVA releases. This marker/Tagged interface concepts are popular trough some books/Web, for some understanding these interfaces most commonly no or very less (let us say one) method(s) described in the interface definitions for identifying to provide the special kind of functionality at runtime. Marker interfaces are for identification of a type of class, weather a class is eligible to get some features on runtime of the application or not. The implemented classes will provide the special functionalities on verifying the class type.

Let s take Serializable as an example one, the ObjectOutputStream class will verify before writing the bytes into output streams, weather the received object is of type Serializable or not. Is the object is not implemented serializable then it will throw the exception NotSerializableException. Now take the another example for marker/tagged interface Cloneable, the clone method of Object s class is the native method which provides the cloned object when it invoked on any instance which implements Cloneable interface.

  • Autor: Roto2
  • Comments: 0
  • Views: 0