tags:

views:

236

answers:

1

if I have a code which has the clone() method but as I know that the duplicate code is bad here(in that application e.g.bank Application),what should I do??? the clone() method is in the bankOfAmerica which implements IAccount(IAccount has the clone() method header!)One person has override the clone() method in the bankOfAmerica class and I don't want to make duplicate code here.what should I do with that clone() method.

clone() method:

public IAccount clone() throws CloneNotSupportedException {
      return (BankOfIranSavingsAccount) super.clone();
}
+1  A: 

Returning 'super.clone()' as the implementation of clone is almost guaranteed to be wrong. If you look at clone's javadoc, you'll see that the expectation is that if b is a clone of a, then the following should typically be true

a != b;
a.getClass() == b.getClass();
a.equals(b);

While these aren't hard and fast rules, and the meaning of clone is really dependent on the implementing class, you should have a really good reason, preferably well documented, if your implementation doesn't meet these expectations. And in general, any clone method that calls super.clone() to create its return value will fail the second test.

Jherico