Note that there are some explanatory texts on larger screens.

plurals
  1. POjava, initialize SubClass from SuperClass
    primarykey
    data
    text
    <pre><code>public class Base { //long list of attributes // no Constructor using fields // no init methode // i cannot change this class } </code></pre> <p>now i extended the Base Class like:</p> <pre><code>public class subClass extends Base{ private boolean selected; ... getter und setter ... } </code></pre> <p>i become a list of Base object <code>List&lt;Base&gt;</code> </p> <p>but i need the same list but as <code>List&lt;SubClass&gt;</code> is there a way to initialize the Subclass from the Base Class?</p> <p>example:</p> <pre><code>for(Base b: list){ SubClass sub = (SubClass)b; // Thats wrong i know if(...){ sub.setSelected(true); } newList.add(sub); } </code></pre> <p>i try to avoid the manual init of each Attribute of the Base <code>Class</code> to the SubClass</p> <p>i update my Question as requested in the Comments: the Design above is just an example. my QUESTIN EXACTLY IS:</p> <p>why converting BaseClass into SubClass (sence <code>Subclass extends BaseClass</code>) is not Possible? why Java dosn't allow me to do the following:</p> <p>example:</p> <pre><code>Class Base{ private String name; ..... } Class SubClass extends Base{ private String title; } </code></pre> <p>then</p> <pre><code>Base b = DBController.getById(...); SubClass sub = (SubClass)b; </code></pre> <p>after that the Object <code>sub</code> should have the Attribute Name from the Object <code>b</code> and the <code>title</code> Attribute is null</p> <p>why is this not the case in java?</p> <p>sorry for my bad english, thanks</p>
    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. 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