Sobes.tech
Middle

Kas abstraktses klasses saab luua konstruktorid, millel on parameetrid?

sobes.tech AI

Vastus AI-lt

In an abstract class, you can create constructors with parameters. The abstract class can have any constructors — with or without parameters. This is necessary so that, during inheritance, 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) {}
}

Therefore, the presence of constructors with parameters in an abstract class is a common practice for passing data to the base part of the object.