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 6587217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:53:23+00:00 2026-05-25T16:53:23+00:00

How to create an object of a particular sub-class of an abstract class based

  • 0

How to create an object of a particular sub-class of an abstract class based on the classNameString generated on runtime? Let say there is an abstract class A

public abstract class A {
    abstract protected void method();               
    A getNewInstance() throws InstantiationException, IllegalAccessException{
        return this.getClass().newInstance();
    }
}

Let there be N sub-classes viz A1, A2,.., AN. There is a need to write following method which would return a subclass object based on classNameString

A getSubClassObject(String classNameString)

I have following two ugly implementations
First:

A getSubClassObject(String classNameString){
    A obj = null;
    if(classNameString.equals("A1")){
        obj = new A1();
    }else if(classNameString.equals("A2")){
        obj = new A2();         
    }
    ...
    }else if(classNameString.equals("AN")){
        obj = new AN();         
    }
    return obj;
}

Second:

A getSubClassObject(String classNameString){
    A obj = null;
    try {
       obj = this.subClassObjectsHashMap().get(classNameString).getNewInstance();
    } catch (InstantiationException e) {
       e.printStackTrace();
    } catch (IllegalAccessException e) {
       e.printStackTrace();
    }
    return obj;
}
private HashMap<String, A> subClassObjectsHashMap(){
    HashMap<String, A> subClassObjectsHashMap = new HashMap<String,A>();
    subClassObjectsHashMap.put("A1", new A1());
    subClassObjectsHashMap.put("A2", new A2());
    ....
    subClassObjectsHashMap.put("AN", new AN());
    return subClassObjectsHashMap;
}

Are there any better ways to solve this problem?

  • 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-25T16:53:23+00:00Added an answer on May 25, 2026 at 4:53 pm

    Yes, if all the constructors receive the same parameter (in your example, no paramaters) you can do

    Class clazz = Class.forName("fully.qualified.class.nane");
    A a = (A) clazz.newInstace();
    

    Both methods can throw various exceptions, so you need to add some catch blocks.

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

Sidebar

Related Questions

In an extension method how do a create an object based on the implementation
I've created the instance Lassie of the class Dog within a particular sub in
I have a project that requires user-defined attributes for a particular object at runtime
I want to dynamically create object of HtmlDivElement in my jsf managed bean and
I want to create object on the client side of aspx page. And i
I'm trying to use the Rails Console to create object instances of my models.
I currently create an object on page load with all possible key/value pairs and
I tried to create dynamic object to validate my config in fly and present
I want to create JSON object using tow query in PHP , Then retrieve
If I create an object inside of the main scope: INDEX.PHP: $db = new

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.