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.
As far as I know marker interfaces get their behaviour by "utility" classes. For example if you have read the Design Patterns book by the Gang of Four, you have behaviour of a class tagged with a marker interface defined in another class.
For example you have a marker interface "Saveable". You have a process that traverses all classes implementing this interface (via reflection) and then does save operation on it.
Yes We can create our own Marker interface..See following one...
Marker interface has no method. Java has built-in marker interface like
Serializable
,Clonable
&EventListner
etc that are understand by JVM.We can create our own marker interface, but it has nothing to do with JVM, we can add some checks with
instanceOf
.Create the empty interface
Write a class and implements the interface
Main
class to check the marker interfaceinstanceof
ObjectOutputStream
andObjectInputStream
will check your class whether or not it implementesSerializable
,Externalizable
. If yes it will continue or else will thrownNonSerializableException
.Create an interface without any method and that is your marker interface.
Sample
If any class which implement this interface will be taken as database entity by your application.
Sample Code:
The whole idea of Marker Interface Pattern is to provide a mean to say "yes I am something" and then system will proceed with the default process, like when you mark your class as Serialzable it just tells that this class can be converted to bytes.
As explained very nicely in the Wikipedia article Marker interface pattern, a marker interface is a form of metadata. Client code can test whether an object is an instance of the marker interface and adapt its (the client's) behavior accordingly. Here's a marker interface:
Then code can test whether an object is a
CoolObject
and do something with it:The
Serializable
interface is defined as part of the Java language. You cannot implement behavior at that level yourself.You can add methods to a marker interface, but that mixes the marker pattern with other conceptual uses for interfaces and can be confusing. (Is a class implementing the interface for the purposes of marking it, or for its behavior, or both?)
As explained in the Wikipedia article, marker interfaces in Java can (and probably should) be replaced with annotations.
Serialization is handled by the
ObjectInputStream
andObjectOutputStream
classes. 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.