can marker interface like serializable contain default methods?

前端 未结 4 1795
终归单人心
终归单人心 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:53

    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.

提交回复
热议问题