Avoid Overloading Clone

ID

java.clone_no_overload

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

clone, reliability

Description

Reports classes that have more than one method named clone(). Overloading clone() creates confusion and violates the Cloneable contract.

Rationale

The Cloneable contract expects a single clone() method with no parameters returning Object. Having multiple clone methods with different signatures misleads callers about which one the Cloneable mechanism invokes.

// Bad — overloaded clone
public Object clone() throws CloneNotSupportedException { return super.clone(); }
public void clone(int depth) { /* ... */ }

Remediation

Keep only the standard clone() signature. Use a descriptive factory method name for alternative copy strategies:

// Good — single clone, named factory for variants
public Object clone() throws CloneNotSupportedException { return super.clone(); }
public MyClass deepCopy(int depth) { /* ... */ }