Javatpoint Logo
Javatpoint Logo

Marker Interface in Java

In this section, we will discuss about marker interface in Java, its uses, built-in (Serializable, Cloneable, and Remote Interfaces) and custom marker interface with examples.

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.

  • Internal Flags: It can be used in place of marker interface to indicate any specific operation.
  • Annotations: Since Java 5, marker interfaces are omitted. Instead of marker interface, Java 5 provides the annotations to achieve the same results. It allows flexible metadata capability. Therefore, by applying annotations to any class, we can perform specific action.

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

In Java, built-in marker interfaces are the interfaces that are already present in the JDK and ready to use. There are many built-in marker interfaces some of them are:

  • Cloneable Interface
  • Serializable Interface
  • Remote Interface

Let's discuss one by one in detail.

Cloneable Interface

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.

Product.java

Output:

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

Serializable Interface

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.

Marker Interface in Java

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

Employee.java

SerializationExample.java

Output:

Data has been read from the file.

Example of Deserialization

Let's deserialize the object state.

DeserializationExample.java

Output:

1187345 Andrew

Remote Interface

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:

  • By extending the UnicastRemoteObject class
  • By using the exportObject() method of the UnicastRemoteObject class

AddAllRemote.java

Create and Start the Remote Application

Server.java

Create and Start the Client Application

Client.java

Custom Marker Interface

Apart from built-in marker interface, Java also allows us to create own marker interface. Let's see an example.

CustomMarkerInterfaceExample.java

Output:

Car is a vehicle.
Yes, engine is working.





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA