Search code examples
javainterfacejava-8marker-interfacesdefault-method

can marker interface like serializable contain default methods?


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.


Solution

  • 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.