Marker Interface in Java
What is marker interface?
An interface that does not contain methods, fields, and constants is known as marker interface. In other words, an empty interface is known as marker interface or tag interface. It delivers the run-time type information about an object. It is the reason that the JVM and compiler have additional information about an object. The Serializable and Cloneable interfaces are the example of marker interface. In short, it indicates a signal or command to the JVM.
The declaration of marker interface is the same as interface in Java but the interface must be empty. For example:
There are the two alternatives of marker interface that produces the same result as the marker interface.
Uses of Marker Interface
Marker interface is used as a tag that inform the Java compiler by a message so that it can add some special behavior to the class implementing it. Java marker interface are useful if we have information about the class and that information never changes, in such cases, we use marker interface represent to represent the same. Implementing an empty interface tells the compiler to do some operations.
It is used to logically divide the code and a good way to categorize code. It is more useful for developing API and in frameworks like Spring.
Built-in Marker Interface
Let's discuss one by one in detail.
Cleanable interface in Java is also a marker interface that belong to java.lang package. It generates replica (copy) of an object with different name. We can implement the interface in the class of which class object to be cloned. It indicates the clone() method of the Object class. If we do not implement the Cloneable interface in the class and invokes the clone() method, it throws the ClassNotSupportedException.
Note that a class that implements the Cloneable interface must override the clone() method with a public method. Let's see an example.
Enter product ID: 139872 Enter product name: Printer Enter product Cost: 3459.67 -------Product Detail-------- Product ID: 139872 Product Name: Printer Product Cost: 3459.67
It is a marker interface in Java that is defined in the java.io package. If we want to make the class serializable, we must implement the Serializable interface. If a class implements the Serializable interface, we can serialize or deserialize the state of an object of that class.
Serialization (converting an object into byte stream) is a mechanism in which object state is read from the memory and written into a file or database. Deserialization (converting byte stream into an object) is the opposite of serialization means that object state reading from a file or database and written back into memory is called deserialization of object.
Serialization (writing) can be achieved with the ObjectOutputStream class and deserialization (reading) can be achieved with the ObjectInputStream class.
Let's see example of serialization and deserialization.
Example of Serialization
Data has been read from the file.
Example of Deserialization
Let's deserialize the object state.
Remote interface is a marker interface that belong to java.rmi package. It marks an object as remote that can be accessed from another machine (host). We must implement the Remote interface if we want to make an object as remote. It identifies the interfaces whose methods can be invoked from a non-local JVM. Any remote object must implement the interface directly or indirectly.
Let's define a Remote interface and implement it in a Java program.
Defining Remote interface
Implement the Remote Interface
There are following two ways to implement the Remote interface:
Create and Start the Remote Application
Create and Start the Client Application
Custom Marker Interface
Apart from built-in marker interface, Java also allows us to create own marker interface. Let's see an example.
Car is a vehicle. Yes, engine is working.