If you want to make a deep copy, then you need to also clone B when you clone A (or create another B instance using, for example, a copy constructor). Object.clone()
simply creates a new instance of the objects containing references to the same objects as the object being cloned. It's a shallow copy. There is no way to correctly make a generic deep copy, simply because it often doesn't make sense:
- cloning a String, for example, is useless since Strings are immutable
- cloning a Socket, for example, makes no sense as a Socket is not clonable, since it represents an active side of a TCP connection, that can't be duplicated.
You should generally avoid using Cloneable. Prefer copy constructors instead. And favor immutability rather than deep copies.