Difficulty: Easy
Correct Answer: An interface with no methods or constants that marks a class as having a particular property so that the JVM or libraries can treat it specially.
Explanation:
Introduction / Context:
Marker interfaces are a classic concept in Java. They appear in many core libraries and are often discussed in interviews to test understanding of how metadata and type information can influence runtime behavior. Examples include Serializable and Cloneable, which signal to the JVM or to framework code that certain operations are allowed on implementing classes.
Given Data / Assumptions:
Concept / Approach:
A marker interface is an interface that declares no methods or constants but serves to tag a class with a specific meaning. When a class implements such an interface, code that knows about the marker can check whether an object is an instance of that interface and then enable or disable certain behavior. The marker therefore acts as a form of metadata at the type level, using the type system instead of annotations or configuration files.
Step-by-Step Solution:
Verification / Alternative check:
By reading the source or documentation for Serializable, it becomes clear that the interface has no methods. Yet serialization code checks whether an object implements Serializable before attempting to serialize it. This confirms the marker behavior. Similar logic appears in support for cloning through Cloneable.
Why Other Options Are Wrong:
Common Pitfalls:
One pitfall is assuming marker interfaces are obsolete because annotations exist. Although annotations are often preferred in new designs, existing libraries still rely on marker interfaces. Another mistake is to add methods later to a marker interface, which breaks the original guarantee that existing classes can implement it without adding new code.
Final Answer:
The correct choice is An interface with no methods or constants that marks a class as having a particular property so that the JVM or libraries can treat it specially. because this describes the essential structure and purpose of a marker interface in Java.
Discussion & Comments