I learnt that Object.clone()
and Cloneable
have some bad design decisions. However I have some more doubts:
Why
Object.clone()
has protected modifier, while others likeObject.toString()
,Object.equals()
are notprotected
? Why to makeclone()
inaccessible to non subclasses from different packages and not to keep them public liketoString()
andequals()
?This page says:
Object clone is a protected method, so we will have to override it to use with other classes.
I feel above fact is plain wrong as
protected
modifier does not impose any such restriction. We can still callclone()
, but it will throw run time exception, but not compile time exception.- How every class in Java is sub class of
Object
class? Most likely answer is "by design". But I wanted to know where this fact is enforced? Usually to extend a class, we useextends
keyword. But we dont make every custom class doextends Object
. Then where does it happen? Does compiler auto generates bytecode by enforcing such restriction?