-1

I've scoured the internet for a more fundamental solution to Object deep copy that doesn't require serialization or other external Java tools. My question is how do you deep copy objects that have inherited properties and, in addition are aggregated from other objects? (Not sure if I stated that correctly..)

This is NOT the same question as: "What is the difference between shallow copy and deep copy" because there are specific parameters for this type of deep copy that involves inheritance and aggregation (relying on other objects as instance variables for other objects).

Zachary L
  • 21
  • 3
  • possible duplicate of [What is the difference between a deep copy and a shallow copy?](http://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy) – Andremoniy Mar 18 '15 at 12:05
  • The post 'What is the difference between a deep copy and a shallow copy" is not a duplicate as far as I can tell. That post thoroughly discusses deep copy and shows many different examples but none that I can see that are in this format. If there is an example duplicate I will be happy to remove this post. – Zachary L Mar 19 '15 at 15:26

1 Answers1

0

I found a solution to this that works with both inheritance and aggregation - Here is a simple object structure:

//This is our SuperClass
public class Human {

    private String hairColor; //Instance variable

    public Human (String color) { // Constructor
        hairColor = color; 
    }

    public String getHairColor () { //Accessor method
        return hairColor;
    }
}

//This is our beard object that our 'Man' class will use
public class Beard {

    private String beardColor; // Instance variable

    public Beard (String color) { // Constructor
        beardColor = color;
    }

    public String getBeardColor () { // Accessor method
        return beardColor;
    }
}

// This is our Man class that inherits the Human class attributes
// This is where our deep copy occurs so pay attention here!
public class Man extends Human {

    // Our only instance variable here is an Object of type Beard
    private Beard beard;

    // Main constructor with arg
    public Man (Beard aBeard, String hairColor) {
        super(hairColor);
        beard = aBeard;
    }

    // Here is the COPY CONSTRUCTOR - This is what you use to deep copy
    public Man (Man man) {
        this(new Beard(man.getBeard().getBeardColor()), man.getHairColor());
    }

    // We need this to retrieve the object Beard from this Man object
    public Beard getBeard () {
        return beard;
    }
}

Sorry this example wasn't very unisexy.. it was funny to me at first.

The Beard object was thrown in just to show a more difficult example that I struggled with when I was working with inheritance and deep copy. That way when you're using aggregation you'll know what to do. If you don't need to access any objects and just primitives it's a little bit more simple, you can just use the class instance variables.

public class Man extends Human {

    private String moustache;
    private double bicepDiameter;

    public Man (String aMoustache, double bicepSize) {
        this.moustache = aMoustache;
        this.bicepDiameter = bicepSize;
    }

    // This is the simple (no-object) deep copy block
    public Man (Man man) {
        this(man.moustache, man.bicepDiameter);
    }
}  

I really hope this helps! Happy coding :)

Zachary L
  • 21
  • 3
  • shouldnt it be: __public Man (Beard aBeard, String hairColor){ super(hairColor); beard = aBeard; }__ and __this(new Beard(man.getBeard().getBeardColor()), man.getHairColor());__ – huidube Mar 18 '15 at 12:41