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

6

6

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!

Downcomer answered 10/8, 2010 at 23:32 Comment(0)
C
7

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.

Caporetto answered 11/8, 2010 at 0:5 Comment(0)
V
4

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.

Vanderhoek answered 10/8, 2010 at 23:39 Comment(1)
#2 - or a constructor from a nested type, as per Dan's answer.Howl
M
3
  1. Very little. The public constructor can only be used as a protected one.

  2. Yes, it can be called ('sideways') with the this keyword from other (protected/public) constructors.

Moschatel answered 10/8, 2010 at 23:38 Comment(4)
In 2: You mean the "this" keyword. "base" is in derived classes and can't call private ctors.Collision
@John, "base" can call private constructors if the deriving type is a nested class of the derived type.Caporetto
@Dan - Yes, technically that would work, but who would ever do that?Collision
@John, it's the only way I know of to have inheritance without allowing other users to subclass your class (an internal constructor is close and might be preferable if the desired coupling is not quite as tight.) Eric Lippert mentioned that he'll sometimes use this pattern, as well: #1083532Caporetto
D
0

To clarify my first questions, I will answer again(without commenting). So, I know that is not possible to instansite abstract class directly. Only if a class derives from the abstarct class, can be instansiated. I know that a class can have constructor like private, public, internal and also protected. What protected mean, I know.

But what the difference between a public a protected constructor in an abstract class. I can not see the difference beteween there twos.

Downcomer answered 11/8, 2010 at 14:37 Comment(0)
R
0

In case of an inner class, a private constructor will help. See example below:

public abstract class Vehicle
{
    private readonly string LicensePlate;

    private Vehicle(string licensePlate)
    {
        this.LicensePlate = licensePlate;
    }

    internal abstract string VehicleType();

    internal string GetLicensePlate()
    {
        return this.LicensePlate;
    }

    public class Car : Vehicle
    {
        public Car(string licensePlate) : base(licensePlate)
        {
            // Vehicle
        }

        internal override string VehicleType()
        {
            return "Car";
        }
    }

    public class Motorcycle : Vehicle
    {
        public Motorcycle(string licensePlate) : base(licensePlate)
        {
            // Vehicle
        }

        internal override string VehicleType()
        {
            return "Motorcycle";
        }
    }
}
Rebuke answered 8/9, 2023 at 6:31 Comment(0)
M
-1

There doesn't seem to be much difference to me, the following code outputs

Foo
Bar

public abstract class Foo
{
    protected Foo() {
        Console.WriteLine ("Foo");
    }
}

public class Bar : Foo
{
    public Bar() {
        Console.WriteLine ("Bar");
    }
}

void Main()
{
    new Bar();
}

An abstract constructor can not be overriden if it's protected. Not sure If I answered your question :-)

Mcmahan answered 10/8, 2010 at 23:47 Comment(2)
There's no such thing as an "abstract constructor" and you can't override constructors anyway.Howl
That may be so, but if you try the code above you'll see that I have a constructor in an abstract class which indeed executes when a class that derives from it is created.Mcmahan

© 2022 - 2024 — McMap. All rights reserved.