can marker interface like serializable contain default methods?

前端 未结 4 1815
终归单人心
终归单人心 2021-02-14 12:02

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.

4条回答
  •  离开以前
    2021-02-14 12:52

    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.

提交回复
热议问题