Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some questions about abstract class with private, public and protected constructors

Tags:

c#

oop

My first question: What is the difference between an protected and a public constructor in an abstract class?

My second questions: Does it make sense, if the abstract class has an private constructor?

Thanks in advance!

like image 509
Carnation Avatar asked Aug 10 '10 23:08

Carnation


People also ask

Can abstract class have protected constructor?

After all, an abstract class cannot be created directly – only via a derived instance. Consequently, it is only the derived classes for whom it makes sense to access the constructor in the first place. Hence, ReSharper recommends that you make the constructor protected .

Can an abstract class have a constructor?

Constructor is always called by its class name in a class itself. A constructor is used to initialize an object not to build the object. As we all know abstract classes also do have a constructor.

What is the use of private constructor in abstract class?

A private constructor in an abstract class can also serve the purpose of sealed classes (like in Scala or Kotlin etc.). Since you can still provide subclasses from within the abstract class, but outsiders cannot extend/implement (as @Marko Topolnik answered).

Can a class have both private and public constructors?

We can't create public and private constructors simultaneously in a class, both without parameters. We can't instantiate the class with a private constructor. If we want to create an object of a class with private constructor then, we need to have public constructor along with it.


2 Answers

One possible design that would use a private constructor on an abstract class:

public abstract class BaseClass
{
    private BaseClass(Object param)
    {
        //Do something with parameters
    }

    //Provide various methods that descendant classes will know how to perform

    public static BaseClass FromObject(Object value)
    {
        //Based on object, choose which type of derived class to construct...
    }

    private class HiddenDerivedA : BaseClass
    {
        public HiddenDerivedA(Object value)
            : base(value)
        {
        }
    }

    private class HiddenDerivedB : BaseClass
    {
        public HiddenDerivedB(Object value)
            : base(value)
        {
        }
    }
}

This pattern is useful if the derived implementations are tightly coupled to the selection logic used to construct them and you wish to provide a high degree of insulation from the rest of your code. It relieves you of the responsibility of having to support other inheritors besides those you explicitly intended and allows you to expose all private state from the base class to your derived classes.

like image 175
Dan Bryant Avatar answered Nov 14 '22 23:11

Dan Bryant


Question #1: Not much. You can't call the constructor of an abstract class (instantiate it) directly anyway. You could only call one from a subclass, which means you'd definitely have access to protected members as well as public members at that point.

Question #2: No, not much sense. A private constructor on an abstract class could only be called by "constructor chaining" from a non-private constructor in the same class.

like image 33
Drew Wills Avatar answered Nov 14 '22 22:11

Drew Wills