Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Two questions:</p> <ol> <li><p>Do you really need an Array? Arrays don't work great with generics. So an <code>ArrayList&lt;LevelFactory&gt;</code> might be the better solution</p></li> <li><p>Do you really need the downcast to the special type (Level1Factory, Level2Factory)? If they have a common super method which is defined in <code>LevelFactory</code> (lets say <code>Level getLevel()</code>) you should not need to downcast them. Just call <code>getLevel()</code> and you get the correct Level instance from your factory.</p></li> </ol> <p>Another note because this seems to be a common pitfall:</p> <p><code>new Class&lt;? extends LevelFactory&gt;</code></p> <p>This is not a valid statement (it does not matter if its an array or not). <code>&lt;? extends T&gt;</code> is only valid for the type on the left side. It defines that the generic type of the created Object can be T or derived from T. For Collections this does not mean that they can store objects of T or derived from T (which can a Collections of T anyway).</p> <ol> <li><p><code>List&lt;LevelFactory&gt; list = new ArrayList&lt;LevelFactory&gt;()</code> This means you can add objects of LevelFactory, Level1Factory and Level2Factory to <code>list</code>. When you want to receive objects from <code>list</code> they are of type <code>LevelFactory</code>.</p></li> <li><p><code>List&lt;? extends LevelFactory&gt; list = new ArrayList&lt;LevelFactory&gt;()</code> Means you can receive objects of <code>LevelFactory</code> from <code>list</code>. BUT you cannot add any object to <code>list</code> in a typesafe way because you don't know the exact generic type of <code>list</code>. That because you can also assign <code>new ArrayList&lt;Level1Factory&gt;()</code> to <code>list</code>. Which means that you can't even add <code>LevelFactory</code> objects to <code>list</code> because they don't implement <code>Level1Factory</code>.</p></li> </ol> <p>In general <code>&lt;? extends Something&gt;</code> on collections is not what you want in most cases.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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