Tutorial Playlist
In object-oriented programming, interfaces play a crucial role in defining contracts and establishing a common behavior among classes. In Java, interfaces serve as a blueprint for defining methods that classes implementing those interfaces must implement. However, there is a special type of interface called a "marker interface" that does not declare any methods but serves as a special marker or tag for classes. In this article, we will explore the concept of marker interfaces in Java, their uses, built-in marker interfaces, custom marker interfaces, alternatives, and their differences from annotations and typical interfaces.
Marker interfaces, also known as tagging interfaces, are interfaces without any methods. They act as a flag or marker, indicating that a class implementing the marker interface possesses some specific characteristic or behavior. The presence of a marker interface allows other parts of the program to determine or modify the behavior of the marked class. Marker interfaces are typically used to provide metadata or indicate a capability that cannot be deduced from the class hierarchy or other means.
A marker interface is defined as an interface without any methods. Its sole purpose is to "mark" or "tag" a class to indicate a specific characteristic or behavior. Any class that implements a marker interface is considered to possess the indicated capability or characteristic. Let's consider an example to understand this concept better.
javaCopy codeÂ
// Marker InterfaceÂ
public interface Printable {Â
}Â
// Class implementing the marker interfaceÂ
public class Invoice implements Printable {Â
  // Class implementationÂ
}Â
// Usage of the marker interfaceÂ
public class Printer {Â
  public void print(Printable printable) {Â
    // Perform printing operationsÂ
  }Â
} Â
In the above example, the Printable interface is a marker interface. The Invoice class implements this interface, indicating that it possesses the capability of being printable. The Printer class utilizes the marker interface in its print method to handle printing operations for any object that implements the Printable interface.
Marker interfaces serve various purposes in Java programming. They provide a way to add metadata or indicate specific capabilities to classes. Some common uses of marker interfaces include:
Java provides several built-in marker interfaces that serve specific purposes. These interfaces include:
The Cloneable interface serves as a marker interface for classes that support object cloning. It does not declare any methods and acts as an indicator to the Java runtime that the class can be cloned. Here's an example:
javaCopy codeÂ
public class MyClass implements Cloneable {Â
  // Class implementationÂ
} Â
To enable cloning for a class, it must implement the Cloneable interface. The class should also override the clone() method inherited from Object to provide the appropriate cloning logic.
javaCopy codeÂ
public class MyClass implements Cloneable {Â
  // Class implementationÂ
Â
  @OverrideÂ
  protected Object clone() throws CloneNotSupportedException {Â
    return super.clone();Â
  }Â
}Â
The Serializable interface is a marker interface used to indicate classes that can be serialized. Implementing this interface enables objects of the class to be converted into a byte stream, making them eligible for storage, transmission, or persistence.
javaCopy codeÂ
public class Customer implements Serializable {Â
  // Class implementationÂ
}Â
In the above example, the MyClass implements the Serializable interface, enabling instances of this class to be serialized.
The Remote interface is a marker interface in the Java Remote Method Invocation (RMI) system. It is used to mark objects that can be accessed remotely. Methods defined in remote interfaces can be invoked on these remote objects.
javaCopy codeÂ
public interface Calculator extends Remote {Â
  int add(int a, int b) throws RemoteException;Â
}Â
In the above example, the MyRemoteInterface extends the Remote interface. This indicates that this interface represents a remote object, and methods defined in this interface can be invoked remotely.
Developers can create their own marker interfaces to indicate specific characteristics or behaviors in their applications. These custom marker interfaces can be used to enable certain functionalities or trigger specific actions. Let's consider an example of a custom marker interface called Persistable:
javaCopy codeÂ
// Custom Marker InterfaceÂ
public interface Persistable {Â
}Â
Â
// Class implementing the marker interfaceÂ
public class Order implements Persistable {Â
  // Class implementationÂ
}Â
Â
// Usage of the marker interfaceÂ
public class PersistenceManager {Â
  public void save(Persistable persistable) {Â
    // Perform persistence operationsÂ
  }Â
} Â
In the above example, the Persistable interface is a custom marker interface. The Order class implements this c marker interface, indicating that it can be persisted. The PersistenceManager class utilizes the marker interface in its save method to handle persistence operations for any object that implements the Persistable interface.
Marker interfaces have some limitations, such as the inability to add additional methods or properties to the tagged classes. As an alternative, developers often use annotations in modern Java applications. Java Marker Annotations provide a more flexible and extensible mechanism to mark classes and provide additional metadata. Here's a Java interface example comparing a marker interface and an annotation:
javaCopy codeÂ
// Marker InterfaceÂ
public interface Printable {Â
}Â
Â
// AnnotationÂ
@Retention(RetentionPolicy.RUNTIME)Â
@Target(ElementType.TYPE)Â
public @interface Printable {Â
}Â
Â
// Usage of the marker interfaceÂ
public class Invoice implements Printable {Â
  // Class implementationÂ
}Â
Â
// Usage of the annotationÂ
@PrintableÂ
public class Invoice {Â
  // Class implementationÂ
}Â
In the above example, both the marker interface and the annotation achieve the same purpose of marking the Invoice class as printable. However, annotations offer more flexibility by allowing additional information to be provided through annotation parameters.
The uses of inner interfaces have been discussed earlier in this article. They serve as a means to indicate special behaviors or capabilities and enable runtime processing based on their presence. Marker interfaces provide a lightweight mechanism to tag classes with specific characteristics, allowing for better control and decision-making in various scenarios.
Apart from the built-in marker interfaces like Cloneable, Serializable, and Remote, marker interfaces can be created and used in custom scenarios. Here's an example of a custom marker interface called Loggable:
javaCopy codeÂ
public interface Loggable {Â
  // Empty interface, no methodsÂ
}Â
Classes that implement the Loggable interface can be identified as objects that require logging. Frameworks or logging mechanisms can then utilize this marker interface to log specific events or actions for these classes.
Marker interfaces differ from typical interfaces in that they do not declare any methods. They exist solely to mark or tag classes with certain characteristics or behaviors. On the other hand, typical interfaces define a set of methods that implementing classes must override. Typical interfaces focus on defining contracts and providing a blueprint for the behavior of implementing classes.
Marker interfaces and typical interfaces are both used in object-oriented programming, but they serve different purposes. Typical interfaces define a contract for implementing classes and specify the methods that must be implemented. They provide a way to achieve polymorphism and code reusability.
On the other hand, marker interfaces do not contain any methods; they simply act as a marker or flag for the implementing classes. They indicate that a class possesses certain characteristics or capabilities, without adding any specific behavior. Marker interfaces are often used for metadata, reflection, or to enable certain behaviors or optimizations in frameworks or libraries.
In summary, typical interfaces define behavior through methods, while marker interfaces provide a way to add metadata or flags to classes without adding methods.
Marker interfaces in Java serve as special markers or tags to indicate specific characteristics or capabilities of classes. They provide a way to add metadata to classes and enable various functionalities. Built-in marker interfaces like Cloneable, Serializable, and Remote indicate capabilities like cloning, serialization, and remote access. Developers can also create custom marker interfaces to indicate application-specific behaviors. Marker interfaces have their alternatives in the form of annotations, which offer more flexibility and extensibility. Understanding marker interfaces and their uses can enhance the design and functionality of Java applications.
1. Can marker interfaces have methods?
No, marker interfaces do not declare any methods. They exist solely for marking or tagging classes with a specific characteristic.
2. How are marker interfaces different from typical interfaces?
Marker interfaces do not declare any methods and serve as markers or tags for classes, indicating specific characteristics or behaviors. Typical interfaces, on the other hand, define a set of methods that implementing classes must implement, providing a contract for behavior.
3. Can I create my own marker interfaces?
Yes, you can create your own marker interfaces in Java. Custom marker interfaces can be used to indicate specific characteristics or behaviors in your applications.
4. Are marker interfaces still relevant in modern Java development?
While marker interfaces have been widely used in the past, the introduction of annotations in Java has provided a more flexible and extensible mechanism for adding metadata. However, marker interfaces can still be relevant in certain scenarios, especially when working with legacy code or frameworks that rely on them.
5. What are some alternatives to marker interfaces?
Annotations are often considered as alternatives to marker interfaces in modern Java development. Annotations provide a more flexible and extensible way to add metadata and can be processed by annotation processors for additional functionalities.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...