Definition
Marker interface characteristics:
- No methods. The interface body is empty.
public interface Serializable {}. - Tags a capability. Implementing it signals that the class supports a feature.
- Runtime check. Code checks
if (obj instanceof Serializable)to decide whether to serialize. - Examples:
Serializable(Java serialization),Cloneable(Object.clone support),RandomAccess(fast index access).
A bank account with a special stamp on the cover. The stamp (marker interface) says nothing by itself but tells the system this account is eligible for a certain service. A "Serializable" stamp means the account can be saved to file. A "Cloneable" stamp means it can be duplicated.
Understanding the Concepts
What does a marker interface look like?
// JDK marker interfaces (empty bodies)
public interface Serializable {} // can be serialized
public interface Cloneable {} // clone() will not throw CloneNotSupportedException
public interface RandomAccess {} // List supports fast random access (ArrayList vs LinkedList)
How is it used?
public class Account implements Serializable {
private String accountId;
private BigDecimal balance;
}
// ObjectOutputStream checks: is the object Serializable?
if (obj instanceof Serializable) {
oos.writeObject(obj); // allowed
} else {
throw new NotSerializableException(); // rejected
}
Marker interface vs annotation?
| Feature | Marker interface | Annotation |
|---|---|---|
| Defines a type | Yes (usable in generics: ) | No |
| Compile-time checking | Yes (instanceof, type bounds) | Limited |
| Can carry data | No (empty) | Yes (@Entity(name="accounts")) |
| Discoverable at runtime | instanceof | Reflection |
| Modern preference | Less common | More common |
// Marker interface: type-safe
public <T extends Serializable> void save(T entity) { ... }
// Annotation: more flexible but no type bound
@Entity
public class Account { ... }
When to use marker interface vs annotation?
- Use marker interface when you need a type bound for generics or compile-time type checking.
- Use annotation when you need metadata that carries values, or when the behavior is framework-driven.
What should a senior engineer know?
- Serializable is the most important marker. Without it, ObjectOutputStream throws NotSerializableException.
- Cloneable is a poorly designed marker. clone() is on Object, not on Cloneable. The interface only prevents CloneNotSupportedException. Prefer copy constructors.
- In Spring, annotations like @Transactional, @Entity, @Component have replaced most marker interface use cases.
What are common mistakes?
A common mistake is not understanding the concept fully before using it in production banking applications.
Wrong approach:
package com.bank.service;
public class TransferService {
public void transfer(String fromAccount, String toAccount, double amount) {
// WRONG: using double for money
double balance = getBalance(fromAccount);
balance = balance - amount;
}
private double getBalance(String id) { return 10000.0; }
}
Correct approach:
package com.bank.service;
import java.math.BigDecimal;
public class TransferService {
public void transfer(String fromAccount, String toAccount, BigDecimal amount) {
// CORRECT: using BigDecimal for money
BigDecimal balance = getBalance(fromAccount);
balance = balance.subtract(amount);
}
private BigDecimal getBalance(String id) { return new BigDecimal("10000.00"); }
}
Always use BigDecimal for monetary calculations in banking systems to avoid floating-point precision errors.
Visual Overview
Marker interface = empty interface, tags a class
Serializable: Account implements Serializable → can be serialized
Cloneable: Account implements Cloneable → clone() works
RandomAccess: ArrayList implements RandomAccess → fast index access
Runtime check: if (obj instanceof Serializable) { serialize it }
Modern alternative: @Entity, @Transactional (annotations)
Key Takeaway
A marker interface is an empty interface that tags a class with a capability. The JVM or framework checks instanceof to decide behavior. Serializable, Cloneable, and RandomAccess are the classic examples.
Modern Java prefers annotations for metadata because they can carry values and are more flexible. But marker interfaces still have one advantage: they define a type, so you can use them in generics ().
One sentence to remember: a marker interface has no methods – it just tags a class with a capability that the runtime checks via instanceof.
---