Sobes.tech
Junior — Middle

Can you explain what a marker interface is and why it is needed in programming?

sobes.tech AI

Answer from AI

A marker interface is an interface without methods, used to indicate or mark a class with a certain property. In Java, it serves to convey information to the compiler or JVM that the class has a specific characteristic.

An example is the Serializable interface. A class implementing this interface can be serialized, meaning it can be converted into a byte stream for storage or transmission.

public class User implements Serializable {
    private String name;
    private int age;
    // getters and setters
}

Thus, a marker interface helps manage the behavior of objects without adding methods.