Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you have a <code>List&lt;Base&gt;</code>, then you cannot convert it to a <code>List&lt;SubClass&gt;</code>. This is mainly because the list may not contain instances of <code>SubClass</code>. The best you can do is:</p> <pre><code>List&lt;SubClass&gt; newList = new List&lt;SubClass&gt;(); for(Base b: list){ if (b instanceof SubClass) { SubClass sub = (SubClass)b; . . . newList.add(sub); } } </code></pre> <p>Generally, however, when you find yourself doing this kind of thing, there's something wrong with your design. You might want to avoid subclassing <code>Base</code> and using <a href="http://www.artima.com/designtechniques/compoinhP.html" rel="noreferrer">composition instead</a>.</p> <p><strong>EDIT</strong> Based on your comments, it sounds like you want to construct a list of <code>SubClass</code> instances using a list of <code>Base</code> instances as a start. One approach is to define a constructor for <code>SubClass</code> that takes a <code>Base</code> as an argument.</p> <pre><code>public class SubClass extends Base{ private boolean selected; public SubClass() { // default constructor } public SubClass(Base original) { // copy constructor -- initialize some fields from // values in original, others with default values } ... getter und setter ... } </code></pre> <p>Then you can construct your new list with:</p> <pre><code>List&lt;SubClass&gt; newList = new List&lt;SubClass&gt;(); for(Base b: list){ SubClass sub = new SubClass(b); . . . newList.add(sub); } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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