I just want feedback on what one considers 'most elegant design' for a situation like this.

I have an object that is immutable with some (sort of) copy constructors to allow creating a similar but modified version of my immutable object. So something like this:

class ImmutableObject{
   Fee fee;
   Fi  fi;
   Fo fo;

   public ImmutableObject(Fee fee, Fi fi, Fo fo){

   public ImmutableObject(ImmutableObject cloneObj, Fee fee){

   public ImmutableObject(ImmutableObject cloneObj, Fi fi){

   public ImmutableObject(ImmutableObject cloneObj, Fo fo){

I'm now expanding this by creating a sub-class with extra behavior:

 Class ImmutableChild extends ImmutableObject{

   Fum fum;

   ImmutableChild(Fee fee, Fi fi, Fo fo, Fum fum){
      super(fee, fi, fo);

The problem is that I have a collection of 'ImmutableObjects' that contain both original and child classes. When I try to clone them to get a new object with one modifid element My copy constructors will always create a class of parent type 'ImmutableObject' type and completely loose the state of 'fum' if I'm actually cloning the child class.

I can easily refactor this to meet any solution I want, copy constructor, clone methods etc. However, while I can get the behavior I want, I'm not sure what would be considered both elequant and non-ridged solution. What is the cleanest method to allow a method that will create a 'clone' of my object with one value modified which works well with children classes that expand functionality?

  • 2
    What language is this? Many languages have value types, I'm not aware of your purpose in doing this, but perhaps value types meet your express purpose better. Describe the reason you want immutability and we might know what you're after... alternatively, just use Haskell where this behaviour is natural with record syntax. Commented Oct 11, 2013 at 18:55
  • If this is Java, please be aware Object.clone() is considered harmful and should generally be avoided. (See Joshua Bloch's Effective Java for the many reasons to avoid it)
    – Andres F.
    Commented Oct 16, 2013 at 14:03

1 Answer 1


Were it not for the complication of accepting possible modifying arguments you would simply provide a virtual clone() method in ImmutableObject that instantiates and then returns a pointer to an ImmutableObject. Derived classes override the function and instantiate their own derived type but still return it as ImmutableObject.

As for the extra arguments passed to the "copy constructors", the code doing the copying or calling the clone() method is aware of these differences so can simply clone the object and then change the clone if and how required.

You might want to consider carrying simple type information in the base class - an enumeration would do - if you need to downcast a cloned object later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.