How does object class in java use cloneable interface without implementing it?

141 views Asked by At

Attaching the snippet of clone method implementation from object class, although object class does not implement cloneable interface, how is it allowed to use it ? Another one : Why couldn't clone method could stay in cloneable interface instead of object class ? What's the significance and importance aka real time application of marker interface which a usual interface could not have provided.

[clone method from object class] [1]: https://i.stack.imgur.com/QH4Yi.png

1

There are 1 answers

3
rzwitserloot On

although object class does not implement cloneable interface, how is it allowed to use it ?

Search the web for what the cast operator does. It is a Cloneable.

Why couldn't clone method could stay in cloneable interface instead of object class ?

Because interfaces can't declare implementations (since java 1.8 they can via the default mechanism, but Object.clone() predates that by over 20 years or so).

Do not look at the way Cloneable works as an example of good API design. It's not, but the developers of java didn't know any better back when the Cloneable stuff was designed.