DEV Community

Rajesh Bhola
Rajesh Bhola

Posted on

Part 4 - Marker Interfaces & Adapter Classes

In the previous article, we learned how Java handles interface naming conflicts, including method conflicts and variable ambiguity.

In this part, we'll explore two interesting concepts that every Java developer encounters sooner or later:

  • What is a Marker Interface?
  • Why do interfaces sometimes have no methods?
  • What is an Adapter Class?
  • Why does Java provide adapter classes like GenericServlet?

These concepts are frequently asked in Java interviews and are widely used in the Java ecosystem.


Why Learn Marker Interfaces and Adapter Classes?

At first glance, both concepts seem strange.

  • An interface with no methods?
  • A class that implements hundreds of methods with empty bodies?

Once you understand the problems they solve, both concepts become very intuitive.


What is a Marker Interface?

A Marker Interface (also called a Tag Interface or Ability Interface) is an interface that does not declare any methods or constants.

Its purpose is not to define behavior, but to mark a class with a special capability.

Example:

interface Serializable {

}
Enter fullscreen mode Exit fullscreen mode

There are no methods inside the interface.

Yet implementing it gives an object special behavior.


Why Does an Empty Interface Exist?

Normally, interfaces define contracts.

But marker interfaces are different.

They simply tell the JVM:

"Objects of this class have a special capability."

The JVM recognizes the marker interface and provides the required functionality internally.


Common Marker Interfaces in Java

Marker Interface Ability Provided
Serializable Allows an object to be serialized (saved to a file or sent across a network)
Cloneable Allows an object to be cloned
RandomAccess Indicates efficient random access for collections like ArrayList
SingleThreadModel (Deprecated) Allowed a servlet to process one request at a time

Real-World Example: Serializable

Suppose you want to save an object into a file.

import java.io.Serializable;

class Customer implements Serializable {

    private String name = "Rajesh";

}
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Explanation

Step 1

Customer implements Serializable.

Step 2

Serializable contains no methods.

Step 3

The JVM recognizes the marker interface.

Step 4

The object can now participate in Java Serialization.

Without implementing Serializable, serialization fails at runtime.


Why Doesn't Serializable Have Methods?

Imagine if it contained a method like:

void serialize();
Enter fullscreen mode Exit fullscreen mode

Every class would have to implement it.

Instead, Java lets the JVM handle serialization automatically.

This greatly reduces programming effort.


Interview Question

Without Methods, How Does a Marker Interface Provide Functionality?

The answer is:

The JVM provides the required functionality internally.

The interface simply acts as a marker that the JVM checks at runtime.


Can We Create Our Own Marker Interface?

Yes.

Example:

interface Auditable {

}
Enter fullscreen mode Exit fullscreen mode

However, simply creating a marker interface does not automatically provide functionality.

For it to be meaningful, your application or framework must check whether a class implements the marker interface and then perform some special action.

For example:

if (object instanceof Auditable) {
    // Perform auditing
}
Enter fullscreen mode Exit fullscreen mode

Unlike Serializable, the JVM does not recognize custom marker interfaces by default.


Advantages of Marker Interfaces

  • Simple to understand
  • No methods to implement
  • Easy to identify special objects
  • Supported directly by several Java APIs

What is an Adapter Class?

Now let's discuss another important concept.

Suppose an interface contains many methods.

interface EventListener {

    void onStart();

    void onStop();

    void onPause();

    void onResume();

}
Enter fullscreen mode Exit fullscreen mode

What if you only need one method?

Java requires you to implement every method.


Without an Adapter Class

class MusicPlayer implements EventListener {

    @Override
    public void onStart() {

    }

    @Override
    public void onStop() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onResume() {

        System.out.println("Music resumed.");

    }

}
Enter fullscreen mode Exit fullscreen mode

Only one method contains useful code.

The remaining methods are empty.

This makes the code unnecessarily long.


The Problem

Imagine an interface containing 100 methods.

Even if you need only one method, Java forces you to implement all 100.

This reduces readability and increases boilerplate code.


The Adapter Class Solution

An adapter class provides empty implementations for every interface method.

Example:

interface EventListener {

    void onStart();

    void onStop();

    void onPause();

    void onResume();

}

abstract class EventAdapter implements EventListener {

    @Override
    public void onStart() {

    }

    @Override
    public void onStop() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onResume() {

    }

}
Enter fullscreen mode Exit fullscreen mode

Now, instead of implementing the interface directly, extend the adapter.

class MusicPlayer extends EventAdapter {

    @Override
    public void onResume() {

        System.out.println("Music resumed.");

    }

}
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Explanation

Step 1

The adapter implements every interface method.

Step 2

Each implementation is empty.

Step 3

Your class extends the adapter.

Step 4

Override only the methods you actually need.


ASCII Diagram

        EventListener
               │
               │ implements
               ▼
        EventAdapter
               │
               │ extends
               ▼
         MusicPlayer
Enter fullscreen mode Exit fullscreen mode

Real-World Example

One of the best-known adapter classes in Java is GenericServlet.

Servlet Interface
        │
        ▼
GenericServlet
        │
        ▼
HttpServlet
Enter fullscreen mode Exit fullscreen mode

GenericServlet provides default implementations, allowing HttpServlet to override only the methods it needs.

Historical Note: Adapter classes were especially useful before Java 8. Today, many listener-style interfaces use default methods instead, reducing the need for separate adapter classes.


Marker Interface vs Adapter Class

Feature Marker Interface Adapter Class
Type Interface Abstract Class
Contains Methods No Yes (empty implementations)
Purpose Marks a class with a capability Reduces boilerplate code
Implementation Required No Override only required methods
Example Serializable, Cloneable GenericServlet

Common Beginner Mistakes

Mistake 1: Assuming Marker Interfaces Contain Methods

They don't.

Their purpose is identification, not behavior.


Mistake 2: Thinking Empty Interfaces Are Useless

Marker interfaces provide metadata that can be recognized by the JVM or by frameworks.


Mistake 3: Implementing Large Interfaces Directly

If an adapter class exists, extending it often leads to cleaner and more maintainable code.


Best Practices

  • Use built-in marker interfaces only when their behavior is appropriate.
  • For application-specific markers, ensure your framework or application checks for them.
  • Prefer adapter classes (or modern default methods) when only a few interface methods are needed.
  • Avoid creating large interfaces; follow the Interface Segregation Principle (ISP) by splitting them into smaller, focused interfaces.

Interview Questions

1. What is a Marker Interface?

A marker interface is an interface with no methods or fields that identifies classes having a specific capability.

Why interviewers ask

To test your understanding of Java's type system and legacy APIs.


2. Name Some Marker Interfaces in Java.

Common examples include:

  • Serializable
  • Cloneable
  • RandomAccess

3. Can We Create Our Own Marker Interface?

Yes.

However, it has meaning only if your application or framework checks for it and performs special processing.


4. What is an Adapter Class?

An adapter class is an abstract class that implements an interface by providing empty implementations for all its methods.


5. Why Do We Need Adapter Classes?

They reduce boilerplate code by allowing subclasses to override only the methods they need.


6. Are Adapter Classes Still Relevant After Java 8?

They are less common because default methods in interfaces can often provide default behavior directly. However, you may still encounter adapter classes in older APIs and legacy codebases.


Quick Memory Trick 🧠

Remember MAP:

M → Marker Interface
      │
      ▼
Marks an object

A → Adapter Class
      │
      ▼
Avoids unnecessary code

P → Programming becomes simpler
Enter fullscreen mode Exit fullscreen mode

Think MAP whenever you see these two concepts.


Key Takeaways

  • A marker interface contains no methods or fields.
  • Marker interfaces identify classes with special capabilities.
  • Serializable, Cloneable, and RandomAccess are common examples.
  • Adapter classes provide empty implementations for interface methods.
  • Adapter classes reduce boilerplate by letting subclasses override only what they need.
  • Modern Java often uses default methods instead of adapter classes, but understanding adapters remains valuable for interviews and legacy code.

If you found this guide helpful, leave a ❤️ and follow for more beginner-friendly Java tutorials, interview questions, and practical coding examples.

Happy Coding!

Top comments (0)