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 implement cloneable
  • cloneable, however, not define methods, users of objects can't refer cloneable , expect clone method.
  • every class in hierarchy must call super.clone() default cloning mechanism work

check this question alternatives.


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -