Sobes.tech
Middle

Can abstract class constructors with parameters be created or not?

sobes.tech AI

Answer from AI

Constructors with parameters can be created in an abstract class. An abstract class can have any constructors — both parameterless and with parameters. This is necessary so that when inheriting, derived classes can call these constructors to initialize the base part of the object.

For example, in C#:

abstract class Base
{
    protected int value;
    public Base(int val) { value = val; }
}

class Derived : Base
{
    public Derived(int val) : base(val) {}
}

Thus, having constructors with parameters in an abstract class is a normal practice for passing data to the base part of the object.