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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:49:10+00:00 2026-05-10T13:49:10+00:00

I have a collection of classes that inherit from an abstract class I created.

  • 0

I have a collection of classes that inherit from an abstract class I created. I’d like to use the abstract class as a factory for creating instances of concrete implementations of my abstract class.

Is there any way to hide a constructor from all code except a parent class.

I’d like to do this basically

public abstract class AbstractClass {     public static AbstractClass MakeAbstractClass(string args)     {         if (args == 'a')             return new ConcreteClassA();         if (args == 'b')             return new ConcreteClassB();     } }  public class ConcreteClassA : AbstractClass { }  public class ConcreteClassB : AbstractClass { } 

But I want to prevent anyone from directly instantiating the 2 concrete classes. I want to ensure that only the MakeAbstractClass() method can instantiate the base classes. Is there any way to do this?

UPDATE
I don’t need to access any specific methods of ConcreteClassA or B from outside of the Abstract class. I only need the public methods my Abstract class provides. I don’t really need to prevent the Concrete classes from being instantiated, I’m just trying to avoid it since they provide no new public interfaces, just different implementations of some very specific things internal to the abstract class.

To me, the simplest solution is to make child classes as samjudson mentioned. I’d like to avoid this however since it would make my abstract class’ file a lot bigger than I’d like it to be. I’d rather keep classes split out over a few files for organization.

I guess there’s no easy solution to this…

  • 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. 2026-05-10T13:49:10+00:00Added an answer on May 10, 2026 at 1:49 pm

    You can make the sub classes child classes, something like this:

    public abstract class AbstractClass {     public static AbstractClass MakeAbstractClass(string args)     {         if (args == 'a')             return new ConcreteClassA();         if (args == 'b')             return new ConcreteClassB();     }      private class ConcreteClassA : AbstractClass     {     }      private class ConcreteClassB : AbstractClass     {     } } 

    @Vaibhav This does indeed mean that the classes are also hidden. But this is as far as I am aware the only way to completely hide the constructor.

    Edit: As others have mentioned the same thing can be accomplished using Reflection, which might actually be closer to what you would like to be the case – for example the above method replies on the concrete classes being inside the same file as the Abstract class, which probably isn’t very convenient. Having said that this way is a nice ‘Hack’, and good if the number and complexity of the concrete classes is low.

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

Sidebar

Related Questions

I have an abstract parent class which child classes that inherit from it. I
I have an abstract parent class(Product) and two child classes that inherit from it(Book,Software).
I have a base class called SCO that a number of classes inherit from.
I have a number of LINQ classes that inherit from the same base class.
I have a system.collections.generic.list(of ListBox) I would like to use the collection classes built-in
I have 2 classes that inherits from a father class. The father class has
I've got a number of classes that inherit from Item<T> . Each class has
I have three classes that inherit from both whirlyB and gameCreature . I am
I have a collection of Objective-C classes that get sub-classed at a variety of
I have an APVendor class, which has a collection of APInvoice classes and a

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.