Junior — Middle
Can you give an example of an interface used only as a marker or label without methods?
sobes.tech AI
Answer from AI
In Java, an interface that is used only as a marker or label is called a marker interface. It does not contain methods and serves to indicate a certain property or behavior of a class.
An example of such an interface is java.io.Serializable. A class implementing this interface signals that its objects can be serialized, although the interface itself does not contain any methods:
public interface Serializable {
// No methods
}
public class User implements Serializable {
private String name;
private int age;
// Constructors, getters, setters
}
Here, Serializable is a marker used by the JVM and libraries to determine if an object can be serialized.