Junior — Middle
Why have modern projects abandoned the use of marker interfaces?
sobes.tech AI
Answer from AI
Marker interfaces in Java are interfaces without methods that serve to indicate that a class has a certain characteristic (for example, Serializable). In modern projects, they are often abandoned for several reasons:
- Lack of explicit contract: A marker interface does not contain methods, so it does not guarantee the implementation of any behavior, which reduces its informativeness.
- Flexibility of annotations: With the advent of annotations, it became possible to mark classes more flexibly and explicitly without forcing them to inherit an interface.
- Inheritance issues: A class can implement only one interface with specific behavior, and markers can create confusion with multiple inheritance.
Example: instead of
public class MyClass implements Serializable {}
the modern approach is to use an annotation:
@Serializable
public class MyClass {}
(a hypothetical example, since Serializable is an interface in the standard library, but the idea is that annotations are more expressive).