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

The Archive Base Latest Questions

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

I have no doubt this has been asked and answered here a dozen times,

  • 0

I have no doubt this has been asked and answered here a dozen times, but I just can’t find a satisfactory answer.

I have a family of classes that I only want instantiated by the abstract parent class through a static method like this (I probably have typos here I am typing without trying to compile).

public abstract class Papa {
    public static Papa newInstance() {
        String strClass = Papa.figureOutTheNameOfChildClassToInstantiate();
        Papa papa = (Papa) Class.forName(strClass).newInstance();
        return papa;
    }
    public abstract void doSomething();
    ...
}
public class Child extends Papa {
    public void doSomething() { /* Do something */ }
}

That’s more or less what I have now. What I wish to do is be sure that the Child can only be instantiated by the factory method Papa.newInstance(). I tried to do this by making the Child’s no-argument constructor private, but then Papa can’t instantiate it.

So, my question is, how can I make sure Child instances are only created by the factory method Papa.newInstance()?

  • 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-05T22:57:35+00:00Added an answer on June 5, 2026 at 10:57 pm

    If you absolutely, positively, want to enforce this, then give Papa a protected constructor which takes a parameter of type Xyzzy. Make Xyzzy a protected inner class in Papa. Give it a private constructor. Now, only Papa can make instances of Xyzzy. Since you need an instance of Xyzzy to call Papa‘s constructor, and since subclasses of Papa must call its constructor from their constructors, you need an instance of Xyzzy to call the constructor of any subclass of Papa. Hence, only Papa can call the constructors of its subclasses. Even though Xyzzy doesn’t do anything.

    Subclasses could leak their Xyzzy instance, which would allow other code to call subclass constructors. But you can constrain that too, by making Xyzzy single-use, and only so within the context of a single static factory invocation.

    But to be honest, i wouldn’t bother!

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

Sidebar

Related Questions

This question has been asked again and again, but I have still a doubt.
No doubt elements of this question have been asked before, but I'm having trouble
Though I don't doubt this has been answered I cannot find a great match
Sorry if this has been asked somewhere else, but I have looked all around,
I know that this question has been asked too many times, but I think
This question has no doubt been asked in various forms in the past, but
I guess this question has been asked a lot around. I know Rails can
I know this has been asked before but there is really not a clear
I am sure this Question has been asked a lot of time. But wanted
This question has been asked here before . The accepted answer was probably obvious

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.