<p>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. </p>

<p>Is there any way to hide a constructor from all code except a parent class.</p>

<p>I'd like to do this basically</p>

<pre><code>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
{
}
</code></pre>

<p>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?</p>

<p><strong>UPDATE</strong><br>
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.</p>

<p>To me, the simplest solution is to <a href="https://stackoverflow.com/questions/20185/is-there-a-way-to-make-a-constructor-only-visible-to-a-parent-class-in-c#20200">make child classes as samjudson mentioned</a>. 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.</p>

<p>I guess there's no easy solution to this...</p>

[{'answer_id': 20199, 'author': 'Vaibhav', 'author_id': 380, 'author_profile': 'https://Stackoverflow.com/users/380', 'pm_score': 1, 'selected': False, 'text': "<p>No, I don't think we can do that.</p>\n"}, {'answer_id': 20200, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 3, 'selected': True, 'text': '<p>You can make the sub classes child classes, something like this:</p>\n\n<pre><code>public abstract class AbstractClass\n{\n    public static AbstractClass MakeAbstractClass(string args)\n    {\n        if (args == "a")\n            return new ConcreteClassA();\n        if (args == "b")\n            return new ConcreteClassB();\n    }\n\n    private class ConcreteClassA : AbstractClass\n    {\n    }\n\n    private class ConcreteClassB : AbstractClass\n    {\n    }\n}\n</code></pre>\n\n<p><a href="https://stackoverflow.com/questions/20185/is-there-a-way-to-make-a-constructor-only-visible-to-a-parent-class-in-c#20220">@Vaibhav</a> 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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 20221, 'author': 'Rob Cooper', 'author_id': 832, 'author_profile': 'https://Stackoverflow.com/users/832', 'pm_score': 2, 'selected': False, 'text': '<p>If the classes are in the same assembly, can you not make the constructors internal?</p>\n'}, {'answer_id': 20258, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Do you actually <strong>need</strong> to do this?  If you're using some kind of pseudo factory pattern without a true design need for it, you're only going to make your code harder to understand, maintain and extend.</p>\n\n<p>If you don't need to do this, just implement a true factory pattern.  Or, more ALTy, use a DI/IoC framework.</p>\n"}, {'answer_id': 20266, 'author': 'lubos hasko', 'author_id': 275, 'author_profile': 'https://Stackoverflow.com/users/275', 'pm_score': 3, 'selected': False, 'text': '<blockquote>\n  <p>To me, the simplest solution is to\n  make child classes as samjudson\n  mentioned. I\'d like to avoid this\n  however since it would make my\n  abstract class\' file a lot bigger than\n  I\'d like it to be. I\'d rather keep\n  classes split out over a few files for\n  organization.</p>\n</blockquote>\n\n<p>No problem, just use <strong>partial</strong> keyword and you can split your inner classes into as many files as you wish. You don\'t have to keep it in the same file.</p>\n\n<p><strong>Previous answer:</strong></p>\n\n<p>It\'s possible but only with reflection</p>\n\n<pre><code>public abstract class AbstractClass\n{\n    public static AbstractClass MakeAbstractClass(string args)\n    {\n        if (args == "a")\n            return (AbstractClass)Activator.CreateInstance(typeof(ConcreteClassA), true);\n        if (args == "b")\n            return (AbstractClass)Activator.CreateInstance(typeof(ConcreteClassB), true);\n    }\n}\n\npublic class ConcreteClassA : AbstractClass\n{\n    private ConcreteClassA()\n    {\n    }\n}\n\npublic class ConcreteClassB : AbstractClass\n{\n    private ConcreteClassB()\n    {\n    }\n}\n</code></pre>\n\n<p>and here is another pattern, without ugly <strong>MakeAbstractClass(string args)</strong></p>\n\n<pre><code>public abstract class AbstractClass&lt;T&gt; where T : AbstractClass&lt;T&gt;\n{\n    public static T MakeAbstractClass()\n    {\n        T value = (T)Activator.CreateInstance(typeof(T), true);\n        // your processing logic\n        return value;\n    }\n}\n\npublic class ConcreteClassA : AbstractClass&lt;ConcreteClassA&gt;\n{\n    private ConcreteClassA()\n    {\n    }\n}\n\npublic class ConcreteClassB : AbstractClass&lt;ConcreteClassB&gt;\n{\n    private ConcreteClassB()\n    {\n    }\n}\n</code></pre>\n'}, {'answer_id': 20279, 'author': 'Jedi Master Spooky', 'author_id': 1154, 'author_profile': 'https://Stackoverflow.com/users/1154', 'pm_score': -1, 'selected': False, 'text': '<p>What you need to do is this to prevent the default constructor to be create. The internal can  be change to public if the classes are not in the same assembly.</p>\n\n<pre><code>public abstract class AbstractClass{\n\n public static AbstractClass MakeAbstractClass(string args)\n {\n    if (args == "a")\n        return ConcreteClassA().GetConcreteClassA();\n    if (args == "b")\n        return ConcreteClassB().GetConcreteClassB();\n }\n}\n\npublic class ConcreteClassA : AbstractClass\n{\n  private ConcreteClassA(){}\n\n  internal static ConcreteClassA GetConcreteClassA(){\n       return ConcreteClassA();\n  }\n}\n\npublic class ConcreteClassB : AbstractClass\n{\n  private ConcreteClassB(){}\n  internal static ConcreteClassB Get ConcreteClassB(){\n       return ConcreteClassB();\n  }\n\n}\n</code></pre>\n'}, {'answer_id': 20325, 'author': 'Peteter', 'author_id': 1192, 'author_profile': 'https://Stackoverflow.com/users/1192', 'pm_score': 0, 'selected': False, 'text': "<p>Can't you use the keyword <code>partial</code> for splitting the code for a class into many files?</p>\n"}, {'answer_id': 24338, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 0, 'selected': False, 'text': '<p>If you are using this class in a seperate service assembly, you can use the internal keyword.</p>\n\n<pre><code>public class AbstractClass\n{\n    public AbstractClass ClassFactory(string args)\n    {\n        switch (args)\n        {\n            case "A":\n                return new ConcreteClassA();               \n            case "B":\n                return new ConcreteClassB();               \n            default:\n                return null;\n        }\n    }\n}\n\npublic class ConcreteClassA : AbstractClass\n{\n    internal ConcreteClassA(){ }\n}\n\npublic class ConcreteClassB : AbstractClass\n{\n    internal ConcreteClassB() {}\n}\n</code></pre>\n'}, {'answer_id': 53880, 'author': 'Mark Ingram', 'author_id': 986, 'author_profile': 'https://Stackoverflow.com/users/986', 'pm_score': 1, 'selected': False, 'text': '<p>Following on from the <a href="https://stackoverflow.com/questions/20185/is-there-a-way-to-make-a-constructor-only-visible-to-a-parent-class-in-c#20200">accepted answer</a>, if you had a public interface and made the private classes implement the interface, you could then return a pointer to the interface and anyone outside of your parent abstract class could then use them (whilst still hiding the child classes).</p>\n'}]
['https://Stackoverflow.com/questions/20185', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/392/']