What do you use marker interface for?
Marker interface doesn't contains any methods or method declarations. What's the use of implements these interface. What's logic behind importing these market interfaces like serializable, clonable.
Expert's answerMarker interfaces are used to 'mark' an implementing class as having the capability indicated by the Marker interface and even though the interface does not define any methods to be implemented by the class being marked, for example, as Serializable, the declaration is required to enable the capability. In the case of Seriablizable, you declare that your class implements Serializable and that indicates that the non-transient data members in the class can be written to an ObjectOutputStream. The ObjectOutputStream private method writeObject() contains a series of instanceof tests to determine writeability, one of which looks for the Serializable interface. So declaring that your class implements the Serializable interface means that the writeability test will pass and writeObject() will send the data to the ObjectOutputStream. Declaring that your class implements the marker interface is how you indicate that your software wants to make use of some built in functionality that is enabled simply by declaring that your class implements the interface, even though you don't have to write any code to implement any methods. In the most recent versions of the Java language it is recommended to use annotations rather than marker interfaces to indicate that classes have particular semantics like serializable and clonable. |
Product Guides

Post new comment