• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why clone() method is not directely accessible from any class ?  RSS feed

 
joni novhia
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I wonder on why any user defined class's object not able to invoke clone() method. I think as Object is the superclass of all the claasses that we define, so Object's clone() method should
be directely accessible from any subclass . But until we implement Cloneable its not getting possible. Can anyone explain me why this is so ?

Thanks in advance.





Output :

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method clone() from the type Object is not visible

at StringWrap.main(StringWrap.java:6)





 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't always want your objects to be cloneable. Sometimes you have classes that represent something that should not be cloneable.

Unfortunately, the designers of the Java language used an odd design when they added this feature to class Object. There's a protected clone() method which throws a CloneNotSupportedException, unless the class implements interface Cloneable. Interface Cloneable is a marker interface (an interface without any methods - its only purpose is to indicate that the class that implements it is allowed to be cloned). If you want, you can override the clone() method in your class and make it public.
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A nice article here
 
joni novhia
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:You don't always want your objects to be cloneable. Sometimes you have classes that represent something that should not be cloneable.

Unfortunately, the designers of the Java language used an odd design when they added this feature to class Object. There's a protected clone() method which throws a CloneNotSupportedException, unless the class implements interface Cloneable. Interface Cloneable is a marker interface (an interface without any methods - its only purpose is to indicate that the class that implements it is allowed to be cloned). If you want, you can override the clone() method in your class and make it public.



Hi Jesper, Thanks for the reply.

actually i doubt on the thing that, why this clone() method is not accessible directly from any user defined class's object. As we know, the protected methods can be accessed by classes which inherit the superclass (In our case class Nil inherit class Object).
So , with this rule in mind, if i do this :

Nil n=new Nil();
n.clone(); <----- here's the compiler error that the clone() is not found for n.

I agree that it throws cloneNotSupportedException if i won't implement cloneable. but my point is why this method is not visible for any user defined class ?


Hope you got this now .....
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a method is protected, it can only be accessed by the class itself, subclasses of the class, or classes in the same package as the class.

Your class StringWrap cannot access the clone() method of class Nil, because class StringWrap is not a subclass of class Nil. StringWrap and Nil are in the same package, but the clone() method is defined in class Object, which is in a different package (java.lang) - so the clone method of class Nil is not accessible from class StringWrap.

If you want to make the clone() method of class Nil publicly visible, override it and make it public:

Also, make class Nil implement Cloneable, otherwise you'll get a CloneNotSupportedException when you run the code.

One more thing: Casting to Object is not necessary, because everything is already an Object:
 
joni novhia
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:When a method is protected, it can only be accessed by the class itself, subclasses of the class, or classes in the same package as the class.

Your class StringWrap cannot access the clone() method of class Nil, because class StringWrap is not a subclass of class Nil. StringWrap and Nil are in the same package, but the clone() method is defined in class Object, which is in a different package (java.lang) - so the clone method of class Nil is not accessible from class StringWrap.

If you want to make the clone() method of class Nil publicly visible, override it and make it public:

Also, make class Nil implement Cloneable, otherwise you'll get a CloneNotSupportedException when you run the code.

One more thing: Casting to Object is not necessary, because everything is already an Object:









Ohk ......
got the funda protected things ....
feels like heaven now ..... Thanks a lot Jesper
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!