How to write our own marker interface in Java?

前端 未结 6 851
遇见更好的自我
遇见更好的自我 2021-02-04 06:15

I know marker interface in java. It is used to define a specific behaviour about a class. For example, Serializable interface has the specific ability to store an object into by

6条回答
  •  别跟我提以往
    2021-02-04 06:45

    1. Serialization is handled by the ObjectInputStream and ObjectOutputStream classes. If a class has special serialization needs, the methods to create are outlined in the API. Reflection is used to invoke these methods.

    2. The same way you would write any other interface.

    3. There's nothing stopping you from putting methods in a marker interface.

    The more common practice now is to use annotations to provide the same metadata marker interfaces provide.

提交回复
热议问题