I know marker interface in java. It is used to define a specific behaviour about a class. For example, Serializable interface has the specific ability to store an object into byte stream and its reverse process. But I don’t know where this specific behaviour is implemented, because it doesn’t have any method in it.
- How JVM invoke this specific behaviour?
- How to write our own marker interface? Can you give me a simple user defined marker interface for my understanding?
- Is it possible to have methods in marker interface?
Please guide me to resolve this issue.
Serialization is handled by the
ObjectInputStreamandObjectOutputStreamclasses. If a class has special serialization needs, the methods to create are outlined in the API. Reflection is used to invoke these methods.The same way you would write any other interface.
There’s nothing stopping you from putting methods in a marker interface.
The more common practice now is to use annotations to provide the same metadata marker interfaces provide.