Reputation: 349
Marker interface is an interface without any members. And serializable is one example for that .
Can we define our own marker interface. If yes how can we define functionality for it.?
Upvotes: 2
Views: 2517
Reputation: 1
All empty interface are not marker Interface...If underlying server or container providing functionality at runtime to its implementation class is called marker Interface.. Eg. Runnable interface Run() method providing functionality to its implementation class thread. Runnable is functional as well as marker Interface.
Upvotes: -1
Reputation: 190
If you look carefully on marker interface in Java e.g. Serializable, Clonnable and Remote it looks they are used to indicate something to compiler or JVM. So if JVM sees a Class is Serializable it done some special operation on it, similar way if JVM sees one Class is implement Clonnable it performs some operation to support cloning. Same is true for RMI and Remote interface. In simplest Marker interface indicate, signal or a command to Compiler or JVM.
--> Practically we can create a interface like marker interface with no method declaration in it but it is not a marker interface at all since it is not instructing something to JVM that provide some special behavior to the class when our program is going to execute.
For example: Serializable, Cloneable etc..are marker interfaces
When my program get exceuted, JVM provides some special powers to my class which has implemented the Marker Interfaces.
Upvotes: 0
Reputation: 78
Marker interface is used as a tag to inform a message to the java compiler so that it can add special behaviour to the class implementing it.
Java marker interface has no members in it.
Ex. 1. java.io.Serializable is Marker interface.
Upvotes: 6
Reputation: 674
Marker interface is used as a tag to inform a message to the java compiler so that it can add special behaviour to the class implementing it. Java marker interface has no members in it.
java.io.Serializable is a marker interface.Meaning of class implimenting this interface is something like intimating the java compiler in some way that there is a possibility of serializing this java class.
You can create your own marker interface. Just create a interface without writing anything inside body.
Suppose you've created Editable
marker interface and created many classes implementing this Editable
now when some programmer uses your class, he would come to know from javadoc that your class is editable.
Upvotes: 0
Reputation: 201537
The Marker interface pattern (from the Wikipedia article) provides a means to associate metadata with a class where the language does not have explicit support for such metadata. For example java.io.Serializable
. A class implements the Serializable
interface to indicate that its non-transient data members can be written to an ObjectOutputStream
.
Upvotes: 3
Reputation:
Why not? But it has nothing with JVM.
interface MarkerInterface { }
class MarkerInterfaceImpl implements MarkerInterface {
...
}
public class MarkerInterfaceDemo {
public static void main(String[] args){
MarkerInterfaceImpl markerInterfaceImpl = new MarkerInterfaceImpl()[
if(markerInterfaceImpl instanceOf MarkerInterface) {
...
}
}
}
Upvotes: 4