I would use the Wheel interface, which has only access methods, that is: -
Example
interface WheelInterface { int getSize(); } class Wheel implements WheelInterface {
Now just pass WheelInterface instead of Wheel, and only the constructors of your Axis class will only have access methods available.
Benefits
The advantages of this are the absence of the need for copying; you just provide the contract to the Axis class, and this contract says that it can only get the size, not change it.
Passing the same object reference by value, you do not need to call any copy constructor and do not have to worry about deep and shallow copy semantics. I would not want to use Clone on my wheel, I think it is a little dirty for the reasons mentioned in the comments to other answers.
In an object-oriented template, using an interface to abstract away from what you don't need is also a sign of good design. If your wheel has winter tires, your Axis class probably doesn't even need to be taken care of!
disadvantages
As already mentioned, you can return the interface back to a specific type (as others mentioned, assuming that you know what you are driving towards); this is called downcasting, and I do not recommend it; if this were done in a similar scenario, he probably would not have passed the code check.
source share