Problem
Why can a constructor for an abstract class be written in C#? We can’t instantiate an abstract class, as far as I know… so what is it for? Isn’t it true that you can’t instantiate the class?
Asked by Mulder
Solution #1
Because you might want to instantiate data in the abstract class in a typical method. As a result, classes that derive from that class can call the base constructor.
public abstract class A{
private string data;
protected A(string myString){
data = myString;
}
}
public class B : A {
B(string myString) : base(myString){}
}
Answered by Craig Suchanec
Solution #2
That’s where you made a mistake. An abstract class can, of course, be instantiated.
abstract class Animal {}
class Giraffe : Animal {}
...
Animal animal = new Giraffe();
There’s an Animal instance right there. An abstract class is instantiated by creating a concrete class that is derived from it and then instantiating that. A derived concrete class’s instance is also an instance of its abstract base class. Even if Animal is abstract, an instance of Giraffe is also an instance of Animal.
Because an abstract class can be instantiated, it must have a constructor, just like any other class, to ensure that its invariants are met.
Now, a static class is one that you can’t instantiate, and you’ll note that creating an instance constructor in a static class isn’t allowed.
Answered by Eric Lippert
Solution #3
It’s a means of enforcing a collection of abstract class invariants. That is, regardless of what the subclass performs, you want to ensure that certain properties of the base class are always true… for example:
abstract class Foo
{
public DateTime TimeCreated {get; private set;}
protected Foo()
{
this.TimeCreated = DateTime.Now;
}
}
abstract class Bar : Foo
{
public Bar() : base() //Bar's constructor's must call Foo's parameterless constructor.
{ }
}
Consider a constructor to be the inverse of the new operator. The constructor’s sole purpose is to ensure that an object is in a usable condition before it is used. It’s only that we generally call it through a different operator.
Answered by Rodrick Chapman
Solution #4
It’s there to impose certain initialization logic that must be followed by all implementations of your abstract class, as well as any methods you’ve added to it (not all the methods on your abstract class have to be abstract, some can be implemented).
Any class that derives from your abstract base class must invoke the base constructor.
Answered by Ross Anderson
Solution #5
In most cases, constructors are used to set up the members of the object being formed. Each class constructor in the inheritance hierarchy is normally responsible for instantiating its own member variables, according to the inheritance principle. This makes sense because the variables must be declared before the instantiation can take place.
Because an abstract class is not totally abstract (unlike interfaces), it has both abstract and concrete members, and the members that are not abstract must be initialized, which is done in the abstract class’s constructors, constructors are required. The constructors of the abstract class can only be called from the constructors of derived classes, of course.
Answered by zafar
Post is based on https://stackoverflow.com/questions/5601777/constructor-of-an-abstract-class-in-c-sharp