Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically instantiating abstract subclass
    primarykey
    data
    text
    <p>First I have to say I am real new to Java. So all apologize if my question sound stupid but I haven't been able to find a solution so far...</p> <p>I would like to dynamically instantiate a abstract class subclass.</p> <p>This is the code</p> <pre><code>public class CommandMap implements Handler.Callback { private HashMap &lt;Integer, Class&lt;? extends AbstractCommand&gt; &gt; __commandHashMap; protected CommandMap() { __commandHashMap = new HashMap&lt;Integer, Class&lt;? extends AbstractCommand&gt;&gt;(); } /** * Map an ID to a command * @param what Id used in the Message sent * @param command Command to be executed */ public void mapWhat(Integer what, Class&lt;? extends AbstractCommand&gt; command) { if ( !__commandHashMap.containsKey(what) ) { __commandHashMap.put(what, command); } } /** * Unmap the id/command pair * @param what Id */ public void unmapWhat(Integer what) { if ( __commandHashMap.containsKey(what) ) { __commandHashMap.remove(what); } } public boolean handleMessage (Message message) { // call the corresponding command if ( __commandHashMap.containsKey(message.what) ) { Class&lt;? extends AbstractCommand&gt; commandClass = __commandHashMap.get(message.what); AbstractCommand command = commandClass.getClass().newInstance(); } return true; // for now } } </code></pre> <p>When doing so the part</p> <pre><code>AbstractCommand command = commandClass.getClass().newInstance(); </code></pre> <p>is giving me an error (illegalAccessException and InstantiationException) in my IDE (not at compile time as I haven't tried it yet) </p> <p>so I surround it with try/catch like this</p> <pre><code>public boolean handleMessage (Message message) { // call the corresponding command if ( __commandHashMap.containsKey(message.what) ) { Class&lt;? extends AbstractCommand&gt; commandClass = __commandHashMap.get(message.what); try { AbstractCommand command = commandClass.getClass().newInstance(); } catch (IllegalAccessException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (InstantiationException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } return true; // for now } </code></pre> <p>but then it tells me that the type Class (sent by the newInstance() ) is obvisouly not of type AbstractCommand.</p> <p>When trying to cast Class to AbstractCommand doing</p> <pre><code>AbstractCommand command = (AbstractCommand) commandClass.getClass().newInstance(); </code></pre> <p>It tells me that Class cannot be cast to AbstractCommand.</p> <p>So I was wondering what I am doing wrong ?</p> <p>Thanks again for any help you could provide.</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.
 

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