No, your attempt
public Point1(Point1 other) { _x = other._x ; _y = other._y; }
absolutely normal ... (I adjusted the type of parameter.)
I will be tempted to make the endings _x and _y final and make the class final, but this is because I like immutable types. Others definitely have different opinions :)
Cloning the inheritance hierarchy is somewhat more complicated - each class in the hierarchy must have a corresponding constructor, pass any argument that it sets to the superclass constructor, and then copy only its own fields. For example:
public class Point2 extends Point1 { private int _z; public Point2(Point2 other) { super(other); this._z = other._z; } }
This is not so bad on the implementation side, but if you want to accurately clone Point2 , you need to know its Point2 in order to call the correct constructor.
Implementing Cloneable makes it a little easier, but there are other things to consider around ... basically cloning objects is not as easy as it might seem :) (I'm sure there is an entry in Effective Java for it. If you have no copy, buy it now.)
Jon skeet
source share