Sumit Singh
Sumit Singh

Reputation: 111

Custom Marker Interface in Java

As Serializable and Cloneable is marker Interface (interface which is not having any method), so if we create our own marker interface then what will be the difference between these two.

Upvotes: 0

Views: 602

Answers (1)

LppEdd
LppEdd

Reputation: 21134

Serializable and Cloneable are standard interfaces. They both belongs to "protected" packages (which means you cannot add classes to them - well you can, but it's not something you do everyday).

java.io.Serializable
java.lang.Cloneable

They're recognized by the JVM at runtime.
For example calling clone() on an object which does not implement Cloneable will throw a

java.lang.CloneNotSupportedException

You might want to use marker interfaces for custom runtime checks, or even for compile time processing. However the recommended way is to use annotations.

Upvotes: 0

Related Questions