Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6476241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:48:03+00:00 2026-05-25T06:48:03+00:00

First I have to say I am real new to Java. So all apologize

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:48:04+00:00Added an answer on May 25, 2026 at 6:48 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, I must say that I'm very new to Google Closure, but
First of all, I have to say that I'm going to talk about System.ComponentModel.Component
Let's say I have a first structure like this: typedef struct { int ivalue;
First let me say I have read this useful article thoroughly and am using
Let's say I have 3 point clouds: first that has 3 points {x1,y1,z1}, {x2,y2,z2},
Let's say I have a form that collects a first name and a last
Say I have a htaccess file shared by dev.server and server.site.com. The first domain
Say I have the following: <ul> <li>First item</li> <li>Second item</li> <li>Third item</li> </ul> How
For example: First, say I have a Silverlight app with Windowless=true so that I
I have looked around like crazy but don't get a real answer. I got

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.