tags:

views:

43

answers:

1

If Class A extends class B and class B has already implemented cloneable interface then is it necessary for class A to declare 'clone() throws CloneNotSupportedException' .

I guess it should not be mandatory, as property to clone Objects of class A would automatically be inherited from Class B.

+1  A: 

It is necessary to override clone() if class B defines non-primitve mutable member fields. These need to be deep copied explicitly within B.clone(). If B only contains primitive and/or immutable data members, A.clone() will do the job.

For a more detailed explanation, see this earlier answer of mine to a similar question.

Péter Török
If you're calling _super.clone_ in every _clone_ implementation, then you'll eventually call _Object#clone_. And that method will make shallow copy of all data fields. I think, OP has got it right.
Nikita Rybak
@Nikita, the first version of my answer was incorrect indeed, but I have since updated it, now I believe it is right.
Péter Török