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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:34:00+00:00 2026-06-05T03:34:00+00:00

It is possiable to instantiate child object from parent class without child class name.

  • 0

It is possiable to instantiate child object from parent class without child class name.

For example, I have next classes:

public class A {
   protected int a;

   public A1() {
       a = 0;
   }

   public int getA() {
       return a;
   }

   public static A getObject() {
       // some code which I need to write
   }
}

public class A1 extends A {
    public A1() {
        a = 5;
    }
}

public class A2 extends A {
    public A2() {
        a = 10;
    }
}

Usage example:

A a = A1.getObject();
a.getA(); // return 5

a = A2.getObject();
a.getA(); // return 10

a = A.getObject();
a.getA(); // return 0

A1, A2 it is not all child classes. There are may be unlimited numbers.

How can I write getObject() method to it creates A class childs instanses.

PS:

I just need to initialize child class object, but there are large amounts of child classes and I would not to call “new” for all of them and I would not to write static method to initialize it, also, all childs have same constructors. It is big lack that I can’t create child instance from parent.

  • 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-06-05T03:34:02+00:00Added an answer on June 5, 2026 at 3:34 am

    When you write A a = A1.getObject(),
    you do use child classname (A1).
    So a) your question is misleading and
    b) why can’t you just write A a = new A1()?

    If you want to write A1.getObject(), then you can redefine getObject() in class A1:

    public class A1 extends A {
       public static A getObject() {
         return new A1();
       }
    
    }
    

    Without redefining, there is no way to declare getObject() in class A so that it return objects different classes because A a = A1.getObject() would compile to A a = A.getObject().

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

Sidebar

Related Questions

Is it possible in PHP to instantiate an object from the name of a
Say you have two classes, A and B. Is it possible to instantiate both
class Parent(): def __init__(self): self.child = Child() class Child(): def __init__(self): # get Parent
Is it possible to instantiate a class in a separate thread without a compile
is it possible to instantiate a session from a session cookie? I have a
Is it possible to have a nested class that is public but can only
An abstract base class (ABC) can have data to support the classes that inherit
I have a class that inherits from a base class. This base class exposes
I have half a dozen classes which all extend the same abstract class. The
Possible Duplicate: Instantiate a class with or without parentheses? This is a minor quibble,

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.