Create a child class object based on a parent one

Jeff Hain jeffhain at rocketmail.com
Sun Aug 10 21:12:32 UTC 2014






>Is there a better way to do this? I can think of Unsafe.

You could define a protected copy constructor like this:
protected A(A toCopy) {
    this.x = toCopy.x;
}
and call it from B constructor:
public B(A toCopy) {
    super(toCopy);

}



>Please note I cannot put an A inside B because there are instanceof 
>calls that B must isa A.
You could still put A in B, and override all methods in B to delegate to A,
but that would waste memory (but if having an interface for A, B could just
implement the interface instead of extending A, and that would work well).


-Jeff


More information about the core-libs-dev mailing list