Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically instantiating abstract subclass

Tags:

java

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...

I would like to dynamically instantiate a abstract class subclass.

This is the code

public class CommandMap implements Handler.Callback
{
    private HashMap <Integer, Class<? extends AbstractCommand> > __commandHashMap;

    protected CommandMap()
    {
        __commandHashMap = new HashMap<Integer, Class<? extends AbstractCommand>>();
    }

    /**
     * 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<? extends AbstractCommand> 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<? extends AbstractCommand> commandClass = __commandHashMap.get(message.what);
            AbstractCommand command = commandClass.getClass().newInstance();
        }
        return true; // for now    
    }
}

When doing so the part

AbstractCommand command = commandClass.getClass().newInstance();

is giving me an error (illegalAccessException and InstantiationException) in my IDE (not at compile time as I haven't tried it yet)

so I surround it with try/catch like this

public boolean handleMessage (Message message)
{
//  call the corresponding command
    if ( __commandHashMap.containsKey(message.what) )
    {
        Class<? extends AbstractCommand> 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
}

but then it tells me that the type Class (sent by the newInstance() ) is obvisouly not of type AbstractCommand.

When trying to cast Class to AbstractCommand doing

AbstractCommand command = (AbstractCommand) commandClass.getClass().newInstance();

It tells me that Class cannot be cast to AbstractCommand.

So I was wondering what I am doing wrong ?

Thanks again for any help you could provide.

like image 849
nolazybits Avatar asked Mar 26 '26 00:03

nolazybits


1 Answers

I think what you want instead of

commandClass.getClass().newInstance()

is

commandClass.newInstance()

commandClass is, itself, a Class. Therefore calling getClass() on it will return java.lang.Class, and if you were to instantiate that (you couldn't, but if you could) it wouldn't be assignable to an AbstractCommand. But removing the extra getClass(), you have the class of the command, and when you instantiate it, you'll get an AbstractCommand instance. Everything else seems fine, I think.

like image 117
Ryan Stewart Avatar answered Mar 28 '26 14:03

Ryan Stewart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!