[Solved] If abstract base class contains parameterised constructor (and derived does not) why can’t it be used? [duplicate]

Introduction

Abstract base classes are a powerful tool in object-oriented programming, allowing for the creation of a base class that can be extended by derived classes. However, if an abstract base class contains a parameterized constructor, and the derived class does not, it can be difficult to use the abstract base class. This is because the derived class cannot access the parameterized constructor of the abstract base class, and thus cannot create an instance of the abstract base class. In this article, we will discuss why this is the case and how it can be addressed.

Solution

The reason why an abstract base class with a parameterized constructor cannot be used is because an abstract class cannot be instantiated. An abstract class is a class that is declared with the keyword abstract and is meant to be used as a base class for other classes. It cannot be instantiated directly, and any class that extends it must implement all of its abstract methods. Therefore, a parameterized constructor in an abstract class cannot be used because it cannot be instantiated.


No, you can’t.

It’s a limitation that says ‘each derived class should use (implicitly or explicitly) at least one constructor from base class.

In your example, your child class implicitly has parameterless constructor which implicitly uses parameterless constructor from base.

So, you need to: either setup parameretised constructor in every derived class or delele this constructor from base class.

Or, you can try something like that:

public class AttributeOption : DomainModelBase<AttributeOptionData>
{
    public AttributeOption(AttributeOptionData data) : base(data) { }
}

That’s not what you exactly want, but that what we have.

solved If abstract base class contains parameterised constructor (and derived does not) why can’t it be used? [duplicate]


This question has been asked before and already has an answer. If you are looking for an answer to this question, please check the following link: If abstract base class contains parameterised constructor (and derived does not) why can’t it be used?

In short, an abstract base class cannot be used if it contains a parameterized constructor and the derived class does not. This is because the derived class must implement the constructor of the abstract base class, and if it does not, then the derived class cannot be instantiated.

The reason for this is that the abstract base class is meant to provide a template for the derived class, and the constructor is part of that template. If the derived class does not implement the constructor, then it cannot be used as a template for the derived class.

In addition, the abstract base class is meant to provide a common interface for all derived classes, and the constructor is part of that interface. If the derived class does not implement the constructor, then it cannot be used as a common interface for all derived classes.