Quick revision
The short answer
For classes that may be published as events, I would use a marker interface such as DomainEvent, because it makes events a type: publish(DomainEvent e) refuses anything else at compile time. When events need metadata such as a topic name or a schema version, I would add an annotation on top of the interface rather than replace it, and if the set of event types is closed, I would make the interface sealed.
What to remember
In this answer, we saw that a marker interface turns "may be published" into a type, so publish(DomainEvent e) is checked by the compiler, while an annotation carries values such as a topic and a version and can be placed on more kinds of declarations, at the price of checks that happen at run time or in a custom processor. The usual answer is to use both: the interface as the rule and the annotation as metadata, with a sealed interface when the family of events is closed.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
For classes that may be published as events, I would use a marker interface such as DomainEvent, because it makes events a type: publish(DomainEvent e) refuses anything else at compile time. When events need metadata such as a topic name or a schema version, I would add an annotation on top of the interface rather than replace it, and if the set of event types is closed, I would make the interface sealed.
Why this question is asked
Every system that publishes events needs a way to say which classes are allowed to travel on the bus. Get it wrong and someone publishes an entity with lazy collections, a half-built command object or a plain String, and the problem only shows up when a consumer chokes on it. Interviewers ask this question because it tests whether we understand what a type gives us that metadata does not, and the reverse. In this answer, we will recap marker interfaces and annotations, see what each one buys us in the event example, compare them side by side, and then settle on the combination most teams end up using, including the sealed interface option.
1A Quick Recap of Marker Interfaces and Annotations
A marker interface is an interface with no methods at all. It exists only so that a class can say "I am one of these". The JDK has a few famous ones: Serializable, Cloneable and RandomAccess. Implementing it costs nothing, but it changes the type of the class, and the compiler knows about types.
An annotation is a piece of metadata we attach to a declaration, such as @Deprecated or Spring's @Component. It does not change the type of anything. Code finds annotations later, either at run time through reflection (reading the class's annotations with getAnnotation) or at build time through an annotation processor, a plug-in that javac runs while compiling. An annotation can also carry values, which a marker interface cannot.
2What the Marker Interface Gives Us: A Type the Compiler Checks
Suppose we declare an empty interface DomainEvent and make our publisher accept only that type. Now the compiler does the checking for us.
Notice that the mistake is caught where it is made, in the editor, before any test runs. The type also flows through the rest of the code: a List<DomainEvent> can only hold events, a handler can be written as a method that takes DomainEvent, and a search for implementations of DomainEvent in the IDE lists every event in the system. Subclasses inherit the interface automatically, so nothing can quietly lose its event status.
3What an Annotation Gives Us: Metadata With Values
Now suppose we used an annotation instead. It can say much more than "this is an event": it can name the topic and the schema version, and it can be placed on methods, fields and even packages, not only on types.
The metadata is genuinely useful: the publisher no longer needs a separate table mapping classes to topics. But there is a catch. Because an annotation is not a type, publish() has to take Object, and the check moves from the compiler to run time. A missing annotation becomes an exception in production, or in a test if we are lucky. We could write an annotation processor that fails the build when publish() is called with an unannotated class, but that is code we must write and maintain, and it is harder to get right than a parameter type.
4Comparing the Two Side by Side
The two tools answer different questions. The marker interface answers "what kind of thing is this?", and the annotation answers "what extra facts do we know about it?". The table puts the differences together.
| Question | Marker interface | Annotation |
|---|---|---|
| Is it a type? | Yes: publish(DomainEvent e) | No: publish(Object e) |
| When are mistakes found? | At compile time | At run time, or by a custom processor |
| Can it carry values? | No | Yes: topic, version |
| Where can it go? | Classes and interfaces only | Types, methods, fields, packages |
| Inherited by subclasses? | Always | Only with @Inherited, never via interfaces |
Notice the row about inheritance. A class annotation is inherited by subclasses only when the annotation itself is marked @Inherited, and never through interfaces, so annotations are easier to lose by accident than an implemented interface.
5The Choice: A Marker Interface for the Type, an Annotation for the Metadata
We do not have to choose only one. A common and sensible design uses the interface to decide what may be published and the annotation to describe how. The publisher keeps its typed parameter, and reads the annotation only for routing.
So the compiler guarantees that only events reach publish(), and the annotation stays a convenience: if someone forgets it, the event still goes to a sensible default topic instead of failing. A unit test can scan all DomainEvent implementations and check that each has the annotation, which closes the remaining gap cheaply.
6When the Set of Events Is Closed: A Sealed Interface
If all event types live in one module and we want to know the full list, we can go one step further and make the interface sealed. A sealed interface names every class allowed to implement it, and since Java 21 a switch over it can be checked for completeness.
This gives consumers a compile error, rather than a silent fall-through, when a new event type appears. It only suits a closed set, though. For a platform where many teams add their own events, a plain DomainEvent interface is the right level, and each team can seal its own family underneath it.
7Follow-up questions
Spring's ApplicationEventPublisher accepts any Object. Does that make the marker pointless?
No. Since Spring 4.2, publishEvent takes Object, which is convenient but means Spring cannot stop us publishing the wrong thing. Wrapping it in our own EventPublisher that takes DomainEvent puts the compile-time check back, and it is also the right place to add the topic routing, logging and serialization rules our system needs.
Is Serializable a good example of a marker interface?
It is a good example of the mechanism and a cautionary example of the meaning. Serializable says a class may be serialized, but implementing it also opts the class into a hidden constructor and a public wire format. That is why we should keep a marker's meaning narrow and documented: DomainEvent should mean exactly "may be published" and nothing else.
Could an annotation processor give us the same compile-time safety?
Partly. A processor can check that annotated classes follow rules, such as being records or having a version, and fail the build otherwise. It is much harder for it to check every call site of publish(). A parameter type does that for free, which is why the type stays the primary rule.
8Conclusion
In this answer, we saw that a marker interface turns "may be published" into a type, so publish(DomainEvent e) is checked by the compiler, while an annotation carries values such as a topic and a version and can be placed on more kinds of declarations, at the price of checks that happen at run time or in a custom processor. The usual answer is to use both: the interface as the rule and the annotation as metadata, with a sealed interface when the family of events is closed.
Open with
I would use a marker interface, something like DomainEvent, as the thing that decides what can be published, because it is a type. The publisher's method becomes publish(DomainEvent event), so if someone tries to publish a String or a JPA entity, it simply does not compile.
Then, if they want more
It also inherits naturally to subclasses and makes every event easy to find in the IDE. An annotation on its own cannot do that: it is metadata, not a type, so the publisher would have to take Object and check for the annotation with reflection, which means mistakes turn up at run time.
Where annotations are better is carrying information, like the topic name or the schema version, and they can go on methods and fields too.
So in practice I would combine them: the interface defines what an event is, and an optional annotation like @PublishedEvent(topic = "orders", version = 2) describes how to route it, with a unit test that checks every event has it.
And if the family of events is closed, say all order events in one module, I would make that interface sealed so a switch over the events is checked for completeness and adding a new event breaks the consumers at compile time instead of silently.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Marker interface or annotation: which would you use to mark classes that may be...": https://scrivio-interview-library.netlify.app/java/interfaces-and-abstract-classes/591da83891/