DEV Community

Aswin Arya
Aswin Arya

Posted on

What is a Marker Interface in Java? (With Examples)

In Java interviews, especially when discussing OOP concepts and design principles, one commonly asked question is:

What is a Marker Interface in Java?

Let’s understand it clearly with examples and real-world relevance.


πŸ”Ή What is a Marker Interface?

A Marker Interface is an interface that does not contain any methods or fields.

It is used to mark a class and provide special meaning to the Java runtime or JVM.

πŸ‘‰ In simple words:
A marker interface is an empty interface used to indicate that a class has a special capability.


πŸ”Ή Why Do We Use Marker Interfaces?

Marker interfaces are used to:

βœ” Provide metadata to the JVM
βœ” Enable special processing
βœ” Indicate special behavior of a class
βœ” Apply runtime checks


πŸ”Ή Example 1: Serializable Interface

One of the most popular marker interfaces in Java is:

java.io.Serializable
Enter fullscreen mode Exit fullscreen mode

It does not contain any methods.

What does it do?

If a class implements Serializable, it means:

πŸ‘‰ The object of that class can be converted into a byte stream (Serialization).

import java.io.Serializable;

class Employee implements Serializable {
    int id;
    String name;
}
Enter fullscreen mode Exit fullscreen mode

If the class does NOT implement Serializable, serialization will fail.


πŸ”Ή Example 2: Cloneable Interface

Another well-known marker interface:

java.lang.Cloneable
Enter fullscreen mode Exit fullscreen mode

It indicates that the class allows cloning.

class Student implements Cloneable {
    int id;

    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
Enter fullscreen mode Exit fullscreen mode

If a class does not implement Cloneable, calling clone() throws CloneNotSupportedException.


πŸ”Ή How Marker Interfaces Work Internally?

At runtime, Java checks:

if (object instanceof Serializable)
Enter fullscreen mode Exit fullscreen mode

If true β†’ Special processing happens.
If false β†’ Exception occurs.

So marker interfaces are mainly used for runtime type checking.


πŸ”Ή Custom Marker Interface Example

You can also create your own marker interface:

interface Auditable {
}
Enter fullscreen mode Exit fullscreen mode
class Transaction implements Auditable {
}
Enter fullscreen mode Exit fullscreen mode

Then in your application:

if (obj instanceof Auditable) {
    System.out.println("Audit required");
}
Enter fullscreen mode Exit fullscreen mode

πŸ”₯ Marker Interface vs Annotation

Modern Java prefers Annotations instead of marker interfaces.

For example:

@Override
@Deprecated
Enter fullscreen mode Exit fullscreen mode

Annotations provide more flexibility compared to marker interfaces.

However, marker interfaces are still important for understanding legacy systems and interview preparation.


πŸ”Ή Key Points for Interviews

βœ” Marker interface is an empty interface
βœ” Used to mark a class with special behavior
βœ” Example: Serializable, Cloneable, RandomAccess
βœ” Used for runtime checking
βœ” Modern replacement: Annotations


🎯 Interview Follow-Up Questions

Interviewers may also ask:

  • Difference between Marker Interface and Annotation?
  • Can we create custom marker interfaces?
  • Why is Serializable a marker interface?
  • What happens if we don’t implement Cloneable?

Understanding these shows strong command over Core Java internals.


πŸš€ Learn Advanced Java with Real-Time Projects

If you want to master advanced Core Java concepts like:

  • Serialization
  • Cloning
  • Multithreading
  • JVM Internals
  • Spring Boot & Microservices
  • Enterprise Application Development

Then check out:

πŸ”₯ AI powered Java Real Time Projects Online Training in Hyderabad

In this program, you will:

βœ” Work on real-time industry projects
βœ” Understand Java from fundamentals to advanced
βœ” Learn AI-integrated backend development
βœ” Prepare confidently for technical interviews

Strong fundamentals + real-time implementation = Career success πŸš€


Top comments (0)