Sunday, 30 August 2026

Marker Interface/Tag Interface

 Sure. This is easier than it sounds.

What is a Marker Interface?

A marker interface is an interface that has no methods and no fields.

Example:

public interface Serializable {
}

A class can implement it:

class Employee implements Serializable {
    int id;
    String name;
}

Here, Serializable doesn't give Employee any method.

So why use it?

It acts like a tag/label that tells Java: "Treat objects of this class specially."


Simple real-world example 🏷️

Imagine you have employees:

Employee
 ├── Manager
 ├── Developer
 └── Intern

Now imagine you put a tag on some employees:

[CAN_WORK_FROM_HOME]

The tag itself doesn't do anything.

But another system can check:

Does employee have CAN_WORK_FROM_HOME tag?
       │
   ┌───┴───┐
  YES      NO
   ↓        ↓
Allow     Don't allow

A marker interface works similarly.


Example: Serializable

Java has:

public interface Serializable {
}

Suppose:

class Employee implements Serializable {
    int id;
    String name;
}

The interface has no methods.

But when Java sees:

Employee emp = new Employee();

and you try to serialize it, Java checks:

Is Employee Serializable?
        │
    ┌───┴───┐
   YES      NO
    ↓        ↓
serialize   error

If the class doesn't implement Serializable, serialization can fail with NotSerializableException.

So:

class Employee implements Serializable

means:

"This class is allowed to participate in Java serialization."


Another example: Cloneable

Java has:

public interface Cloneable {
}

If:

class Employee implements Cloneable {
}

it tells Java that objects of this class are permitted to be cloned through Object.clone().

Again:

Cloneable
    ↓
No methods
    ↓
Just a TAG
    ↓
"This class supports cloning"

Why not use a normal method?

Because sometimes we don't need to provide behavior.

We only need to identify a category.

For example:

interface Serializable {
}

We don't need:

serialize();
deserialize();

inside the interface to make the tag useful.

The Java runtime/library can simply check:

if (obj instanceof Serializable) {
    // treat specially
}

How does Java check it?

For example:

if (employee instanceof Serializable) {
    System.out.println("Employee can be serialized");
}

Output:

Employee can be serialized

So the interface is basically being used as a classification/tag.


🧠 Easy way to remember

Normal Interface
       ↓
Defines BEHAVIOR
       ↓
Methods
       ↓
"WHAT CAN YOU DO?"

Marker Interface
       ↓
Defines CATEGORY
       ↓
No methods
       ↓
"WHAT TYPE/CATEGORY ARE YOU?"

Examples

Marker interfacePurpose
SerializableObject can be serialized
CloneableObject supports cloning
EventListenerIdentifies event-listener types

🎯 Interview answer

A marker interface is an empty interface used to mark or tag a class so that the JVM, compiler, or Java libraries can treat objects of that class specially. Examples include Serializable and Cloneable. It doesn't define behavior through methods; instead, it provides metadata or classification.

Shortcut:
πŸ‘‰ Marker interface = Empty interface + Tag + Special treatment.

No comments:

Post a Comment