Junior — Middle
Explain the concept of Marker Interface and how it is used in programming.
sobes.tech AI
Answer from AI
Marker Interface is an interface without methods and fields, used for marking or indicating a class with a certain property or behavior. In programming, it is used to pass metadata about the class at compile time or runtime.
For example, in Java, the Serializable interface is a marker: it contains no methods, but its implementation tells the JVM that objects of the class can be serialized.
Example:
public interface Serializable {
// No methods
}
public class User implements Serializable {
private String name;
private int age;
// getters, setters, etc.
}
Using a Marker Interface allows frameworks or libraries to check for the presence of the interface via instanceof and perform special actions (e.g., serialization, cloning, security).