Marker Interface in JavaIn 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.
Uses of Marker InterfaceMarker 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 InterfaceIn 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:
Let's discuss one by one in detail. Cloneable InterfaceCleanable 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 InterfaceIt 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 SerializationEmployee.java SerializationExample.java Output: Data has been read from the file. Example of DeserializationLet's deserialize the object state. DeserializationExample.java Output: 1187345 Andrew Remote InterfaceRemote 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 interfaceImplement the Remote InterfaceThere are following two ways to implement the Remote interface:
AddAllRemote.java Create and Start the Remote ApplicationServer.java Create and Start the Client ApplicationClient.java Custom Marker InterfaceApart 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.
Next TopicStatic Function in Java
|