I think it can't, because marker interface principle is to not have any methods, but since default methods are not abstract I am not sure.
-
What's a "default method"? – Dave Newton Nov 03 '14 at 21:59
-
4@DaveNewton See this link http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html – aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Nov 03 '14 at 22:00
-
13A marker interface is just a word used for a normal interface that has a certain convention (no methods). There is no special language support for marker interfaces. If you follow the conventions exactly (no methods) then the marker interface will have no methods in it and so there will be no need for a default method. If you don't follow the rules exactly then it's not really a marker interface so it really is just a plain old interface. I guess I don't really follow your question. – Pace Nov 03 '14 at 22:07
-
2It's not really a marker interface if it has functionality, is it? Marker interfaces by definition are just markers. It's not clear to me what you're asking. – Dave Newton Nov 03 '14 at 22:07
-
@DaveNewton i changed my question please check again... – aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Nov 03 '14 at 22:10
-
@Pshemo my Question is : marker interface can have default methods? – aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Nov 03 '14 at 22:19
-
4You have to ask yourself: what's the point of a marker interface? I have to admit it, I've never seen the reason why anyone should use a marker interface. (`Serializable` doesn't count for two reasons: firstly, it has hidden functionality associated with it, and secondly, it's hideously flawed.) – biziclop Nov 03 '14 at 22:28
-
7Seriously, just use annotations. Marker interfaces are a completely mistaken idea. Because once you mark a class with an interface, all its descendants will inherit it too. But how could you guarantee they'll behave a certain way? – biziclop Nov 03 '14 at 22:33
4 Answers
A "Marker" interface is just a regular interface as far as Java is concerned. Thus, it can have default methods just as any (Java-8) interface can.
Now, as to whether this violates the principle of a Marker interface, I would have to say yes. A Marker interface should act as a flag of sorts, only identifying that a class meets some external criteria. Now, it can be a Marker interface and have abstract/default methods, but it will no longer purely meet the definition.
From Effective Java (Second Edition):
A marker interface is an interface that contains no method declarations, but merely designates (or “marks”) a class that implements the interface as having some property.

- 1,086
- 12
- 27
-
3As you said, having a default method would violate the Marker interface principle since a Marker Interface is supposed to have neither fields nor methods. It's purpose is to tell the JVM that the implementing class will have some special behavior. Really good answer! – Rafael Saraiva Nov 03 '14 at 22:38
-
1I'm not so sure about "it can have default methods" and "it can be a Marker interface and have abstract/default methods". Doesn't every interface _mark_ the classes that are implementing it? The special thing about "Marker interfaces" is that they only act like meta data in my opinion. – kapex Nov 03 '14 at 22:47
-
@kapep The first part is addressing the fact that you can do it if you want, the compiler won't stop you. As for the second, I believe that a marker interface is defined more by how you use it than anything else, thus allowing for the (ill-advised) possibility of one that also defines methods. – Azar Nov 03 '14 at 22:57
-
@Azar Ok, I would say that you can technically _add_ methods, but that doesn't exactly mean that it can _have_ methods (if by adding methods it would loose the "marker" definition). For the second part, fair enough, since there doesn't seem an exact technical definition. If you use an interface like a marker, you probably can name it as such. – kapex Nov 03 '14 at 23:16
A Marker Interface is a design pattern, so we can start to answer your question by observing just what the definition is:
In earlier versions of Java, Marker Interfaces were the only way to declare metadata about a class. For example, the Serializable Marker Interface lets the author of a class say that their class will behave correctly when serialized and deserialized.
The purpose of a Marker interface, in the context of Java, was to say something about that class. Funnily enough, it marked it as something. An example of this is the Serializable
interface, that did nothing but marked that a Class
was able to be serialized into a String
. The question here is:
Does the definition include functionality?
No, I don't think it does. Functionality is more than just metadata about the class; it helps to define the class itself. It takes that step from metadata to data. So in terms of the design pattern, a marker interface can not define or declare functionality; it can simply make a statement about the implementing Class
.

- 26,815
- 5
- 55
- 89
Although @Azar's answer is correct, we mustn't forget that Effective Java was written before default methods were introduced.
What is a marker interface?
There are two ways of looking at marker interfaces:
- They are interfaces that declare no methods.
- They are interfaces that don't force the implementation of any method.
The "official" definition is the first one but up until Java 7 those two statements were equivalent. It is a recurring pattern in Effective Java that once you publish an interface, you can't add any methods to it because it would force the implementation of the new methods.
However, this is exactly the problem default methods are trying to address: to allow for evolution of interfaces without the need to retrofit all classes implementing them. It also makes the two statements above mean slightly different things: a default method clearly violates statement 1 and by design doesn't violate statement 2.
What does all this mean in practice?
Imagine that you write an XML serialization engine and you create a marker interface XmlSerializable
to go with it:
public interface XmlSerializable {}
So far, so good. But later on you realise that you actually have some classes that need special treatment, they need to provide their own custom converters. So what you may do is something like this:
public interface XmlSerializable {
public static final Map<Class,Class> CONVERTERS = ...
default Class customConverter() {
return CONVERTERS.get(this.getClass());
}
}
Would that stop XmlSerializable
being a marker interface? You can say it still is a marker interface as you don't really add extra behaviour directly to your interface, only extra metadata that influences the behaviour of the serializer engine. On the other hand, this solution allows implementing classes to override customConverter()
, which is slightly dodgy, a marker interface shouldn't allow that. (Then again, is Serializable
and Cloneable
relying on "magic" methods in the implementing class any better? I don't think so.)
Arguably the example above isn't a very good way to solve this kind of problem, you'd probably be much better off using annotations. But that also holds true for most "true" marker interfaces.
tl;dr
We can conclude that an interface with only default methods is more or less equivalent to an empty interface. If you want to make a theoretical distinction and not call it a marker interface, that is of course fine. But there's little practical difference, and given the inherent problems with marker interfaces in general, we should probably avoid them anyway.
-
1While Effective Java and many other such texts are pre-Java 8, I don't think that changes anything. Your #1 "way of looking at marker interfaces" is the explicitly stated definition. While #2 *was* synonymous with #1 in the past, the fact that this is no longer the case does not make #2 a valid definition! I think that you're right, and that the distinction *is* largely theoretical, but I will stick to the definition until newer material is released. – Azar Nov 03 '14 at 23:21
-
1@Azar That's of course your prerogative. However, without going into much detail (this isn't a hermeneutics portal after all), every text has many possible interpretations. You chose to take a very literal one and that's fine. But Effective Java isn't gospel, it's a guide to writing good code based on principles and experience. And if you look at the principles behind the definition, you'll find that the literal interpretation is probably misleading. That said, there's no "correct" level of abstraction, only more or less helpful levels. – biziclop Nov 03 '14 at 23:43
-
Agreed. I wrote my answer based on *my* understanding and opinion of how marker interfaces should be defined and what roles they should have. I happened to find a source to corroborate it, but in general, nothing is written in stone (except perhaps [this](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)). – Azar Nov 03 '14 at 23:58
-
the example with readResolve is weak. readResolve should never be public, so yes, the difference will be huge as the method can be called on every single serialize class and worse it will break the existing impl. that use private or protected methods. – bestsss Nov 07 '14 at 21:33
-
@bestsss You're right, the example is weak but I couldn't think of anything better. It doesn't really affect the main point though. – biziclop Nov 07 '14 at 22:07
A marker interface can have default methods, but having them is nonsensical.
A marker interface differs from a conventional interface in the way it's used. A conventional interface defines methods, both abstract and default. Thus it is sensible for a program to declare variables with that interface as its type, and to call methods of both kinds through a reference of that interface type.
A marker interface, by constrast, is not used for calling methods. It is a piece of meta-information about an object declared through the type system. It is typically used by calling code via an instanceof
expression, or occasionally Class.isAssignableFrom()
. It is pointless to declare a variable whose type is a marker interface, since there's nothing you can do with such a variable.
Examples of marker interfaces in the JDK are Cloneable
, RandomAccess
, and Serializable
.
Now consider the addition of a default method to some marker interface:
interface Marker {
default void foo() { ... }
}
What could the default implementation of foo
do?
Implementations of default methods typically want to operate on this
, and they do so by calling other instance methods on this
. They could call other default methods, but having a bunch of default methods calling each other isn't useful. Eventually, some kind of actual operation on this
must be performed. Since interface methods have no access to state (fields), any actual operations must be performed by abstract method implementations residing in an implementing class. However, in a marker interface there are no such methods.
The default implementation of foo
could call a static method on this interface or on some other class. This is mostly pointless, as such a method would probably be better expressed as a static method in the first place. The implementation could pass this
to a static method, but that method couldn't do anything useful with such a reference, since it has no methods! Well, it might have default methods, but now we're going in circles.
For a default method to be useful on an interface, that interface needs to have abstract methods as well. But if it has abstract methods, it's no longer a marker interface. Thus, it is nonsensical to have default methods on a marker interface.

- 127,867
- 37
- 205
- 259