Why might a team choose a hand-written Prototype interface over Java's built-in Cloneable and Object.clone()?
Object.clone() performs a shallow, field-by-field copy by default, and Cloneable itself is just a marker interface with no methods to enforce — it's easy to implement it incorrectly or forget to override clone() for a class that has mutable fields. A hand-written Prototype<T> interface with an explicit clone() method forces every implementing class to decide, field by field, whether each piece of state needs a deep copy or can be safely shared or copied by value. This makes the deep-versus-shallow decision visible in code review and easy to reason about, instead of relying on inherited default behavior that can silently produce shared mutable state between an object and its clone.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response