oop - Accessing clone() from java.lang.Object -
here cannot understand.
in java.lang.object
clone()
defined protected
modifier. definition can accessed name inside own class definition, name inside class derived it, , name in definition of class in same package.
here sample
class in package, , can't access clone()
object
class. sample
derives implicitly object
, why not able access it? definition doesn't has satisfy both conditions (inside same package , subclass).
public class sample { public object foo() throws clonenotsupportedexception { ... return someobject.clone(); } }
in case, clone()
method not visible because not calling subclass. sample
derives object
, can access own clone()
method, not of other objects.
object clone()
designed several mistakes. not practice use - hard right:
- the assumption not every object clonable default
- if override
clone()
making public, still fail, because each class has implementcloneable
cloneable
, however, not define methods, users of objects can't refercloneable
, expect clone method.- every class in hierarchy must call
super.clone()
default cloning mechanism work
check this question alternatives.
Comments
Post a Comment