• Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing Cloneable Interface  RSS feed

 
Somnath Guha
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If subclass is Cloneable why should I implement Cloneable interface to the base class too?
Another question is when we are overriding inherited Clone method, why we are calling super.clone() method?

Thanks,
Somnath Guha
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somnath Guha wrote:Hi,

If subclass is Cloneable why should I implement Cloneable interface to the base class too?


You don't need to.

Another question is when we are overriding inherited Clone method, why we are calling super.clone() method?


We have to create the copy somehow. The other most likely alternative is to use the new operator. The docs tell us that super.clone() is the correct way, however. I'm not sure why. One possible reason is that every class below it in the hierarchy will then automatically produce the proper class when cloned, without us having to write new versions of clone() at every level.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!