Marker interfaces are a special type of interfaces in Java that does not contain any methods or fields. It is used to mark or identify a class as having a certain characteristic or capability. By implementing a marker interface, a class can gain additional functionality or be recognized by other parts of the program.

Marker Interfaces

In Java, marker interfaces are often used for metadata and behavior enablement. These interfaces do not possess any specific methods or fields, but they can be utilized to categorize classes.

Common Uses of Marker Interfaces Include

1. Serialization

One of the most popular examples where marker interfaces are used in java is Serializable interface.

When a class implements this interface, it means that its objects can be serialized; i.e., it can be converted into byte stream and stored in a file or transmitted over network. The presence of the Serializable interface is checked by the Java serialization mechanism before an object is allowed to support serializing itself.

Consider we have a Person.java class which represents information about a person as shown below:

In this program, the Person class implements the Serializable interface, which allows its objects to be serialized and deserialized.

2. Event Handling


Within event handling marker interfaces can be used to identify classes that can handle specific types of events and the ActionListener interface in Java Swing framework is a marker interface that tells you that a class can handle action events.

Example program illustrating the usage of ActionListener interface:

In this program, the ButtonClickHandler class implements the ActionListener interface, allowing it to handle action events generated by the JButton component.

3. Custom Annotations

Moreover, marker interfaces can be used with custom annotations to offer extra information or actions to code. Annotations are metadata that add on to Java code and give some description of the code or enable certain operations.

For e.g., consider we have a custom annotation called MyAnnotation that marks classes as being eligible for some special processing:

In this program, the MyAnnotation annotation is defined with a @Retention annotation to specify that it should be retained at runtime, and a @Target annotation to indicate that it can be applied to types (classes and interfaces).

Now, we can use this custom annotation to mark classes that are eligible for special processing:

In this case, the MyClass class is marked with the MyAnnotation annotation, allowing it to be recognized by other parts of the program that perform the special processing.

How do objects acquire abilities of Marker Interfaces?

The answer lies in how the other parts of the program utilize these marker interfaces. By implementing a marker interface, a class essentially indicates that it has a particular quality or capability. Now other parts of the program can see if an object implements this marker interface and take decisions based on that information or perform certain actions.

For e.g., consider a marker interface called Drawable that marks classes as being drawable:

Now, let’s say we have a method called drawObject that takes an object as a parameter and checks if it implements the Drawable interface:

In this program, the drawObject method uses the instanceof operator to check if the given object implements the Drawable interface. If it does, the method performs a drawing operation, else, it handles non-drawable objects differently.

By implementing the Drawable interface, a class can gain the ability to draw by the drawObject method, even though the Drawable interface itself does not define any methods.

Refer below sample program Pencil class implements Drawable interface and Eraser class won’t then let’s see, how drawObject method behaves when passed Pencil and Eraser object as parameter.

Conclusion

Marker interfaces are an important and versatile feature of Java which allow classes to mark for a different reasons. They are often utilized for serialization, event handling as well as with custom annotations. To add more behavior to a class or let rest part of program know this class too is implemented by using it as parameters to various methods one should implement such marker interface. No doubt, no methods are there within any marker interfaces but their use in programs enables objects to acquire some traits/abilities.
Sample programs are available at git

If you like it, Share it …

Similar Posts