Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to copy generically superclass instances to subclass instances?
    primarykey
    data
    text
    <p>I have a class hierarchy / inheritance like this:</p> <pre><code>public class A { private String name; // with getters &amp; setters public void doAWithName(){ ... } } public class B extends A { public void doBWithName(){ // a differnt implementation to what I do in class A } } public class C extends B { public void doCWithName(){ // a differnt implementation to what I do in class A and B } } </code></pre> <p>So at one time there is a instance of class A with the initialized field "name". Later I want this instance of A get wrapped into instance of B or C. So the superclasses should be get wrapped with a subclass!</p> <p>How can I make this most efficent with respect to DRY?<br> I've thought about a constructor that does some copying with the getters/setters. But in this case I have to repeat myself - and this doesn't respect anymore to my initial requirement of DRY! </p> <p>So, how can I warp A to B by just initializing B's new fields (with default values) and delegating the rest to a method in A (which knows more than B about which fields of A should be accessed...). </p> <p>In the same way:<br> If A should be wrapped into C only a method in c should init C's 'new' fields, delegate to B's wrap method (which therefore inits B's 'new' fields in C) and at last B delegates to A which copies it's fields to the fields of C).</p> <p>So in the end I have a new instance of C which has the values of A wrapped (and some default init values to the fields which the inheritance hierarchy has added).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload