• Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing the Cloneable Interface  RSS feed

 
Tianna Taylor-Albin
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to implement the cloneable interface of the Product class but I keeping getting an error saying that the clone has protected access in Object. I thought the @Override gave access. Can anyone please tell me what I'm doing wrong? Thanks.
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are cloning objects of Product class....so you need to have Product class implementing Clone interface
...then create its instance and call clone() on that. Currently you are implenting Clone on ProductCloneApp.

Manish
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the @Override annotation just tells the compiler to make sure the method it's annotating actually does override a method from the superclass. Which in this case, it does.

But a method whose body only calls the same method from the superclass is actually pointless. If you didn't write that code, then calling the class's clone() method would still call the clone() method from the superclass, which is Object. That's what inheritance does. So I would suggest just not overriding the clone() method.

Edit: Also, as manish says, make sure you make Product cloneable.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
But a method whose body only calls the same method from the superclass is actually pointless. If you didn't write that code, then calling the class's clone() method would still call the clone() method from the superclass, which is Object. That's what inheritance does.


However Object's clone() is protected, so if you want to provide a public clone method of this type, you have to override Object's one in order to make it public.
But how to implement it? Well, through "super.clone()"

Object's clone's API docu writes:

By convention, the returned object should be obtained by calling super.clone.
If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tianna Taylor-Albin wrote:Can anyone please tell me what I'm doing wrong?

I think everyone else has covered most of it; however, just one final thing: You don't have to return an Object, and it's usually best not to. These days, you can use covariant returns (look it up) to return a typed object, viz:

@Override
public Product clone() throws CloneNotSupportedException {
...

That way you don't have to cast every time you use it. Obviously, the above assumes that the method is in your Product class.

HIH

Winston
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:

Unfortunately, a dream too late...

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!