Frage

I have a very simple question (I guess!) How can clone method be protected in Cloneable interface while interfaces can only declare public methods?

War es hilfreich?

Lösung

Cloneable is a marker interface it doesn't have any methods.

clone method is in Object class, since all objects in Java implicitly extends Object hence its available even if its protected.

If a class doesn't implement Cloneable and its clone method is called it will thrown CloneNotSupportedException

Andere Tipps

The Cloneable interface doesn't actually have any methods defined in it. It's simply a marker interface, similar to Serializable.

It's expected that any object that is actually cloneable will implement this interface, and override the clone() method from Object (to at minimum make it public access).

The Cloneable interface doesn't define any methods.
protected Object clone() is a method in java.lang.Object, which throws an exception if the class doesn't implement Cloneable.

The Cloneable doesn't declare any methods :)

You are thinking of Object, which does declare a clone() method.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top