Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good to have a constructor in abstract class?

Is it good to have a constructor in abstract class?

is it a good programming practice to create constructor of abstract class? since abstract classes can not be initialized, their child classes are initialized.

Following is my class structure.

public abstract class Scheduler
{
    private Storyboard timer;

    protected Scheduler()
    {
        // initialize the timer here.
        timer = new Storyboard();

        this.PollInterval = 60;
    }
}


public class TaskScheduler : Scheduler
{
    public TaskScheduler()
        : base()
    {

    }
}
like image 708
Zain Shaikh Avatar asked Nov 07 '10 08:11

Zain Shaikh


3 Answers

Yes, it's absolutely fine. Just because the constructor can only be called by derived classes doesn't mean it won't be useful. For example, you might have an abstract class which represents a named entity of some kind - it would make sense to take the name as a constructor parameter.

It would probably be worth making the constructor protected, to make it even more obvious that you can't just call it from elsewhere.

Note that there being a constructor (or multiple constructors) in an abstract class does force derived class constructors to go through it, but it doesn't force the derived classes to have the same constructor signatures. For example:

public abstract class NamedFoo
{
    private readonly string name;
    public string Name { get { return name; } }

    protected NamedFoo(string name)
    {
        this.name = name;
    }
}

public class DerivedFooWithConstantName
{
    public DerivedFooWithConstantName() : base("constant name")
    {
    }
}

In this case the derived class constructor is "removing" a parameter (by providing a constant value as the argument to the abstract class constructor) but in other cases it could "add" parameters that it required, or have a mixture.

like image 147
Jon Skeet Avatar answered Nov 16 '22 05:11

Jon Skeet


There is absolutely no reason not to have a constructor in an abstract base class.

The abstract class is initialized and works just like any other class. The abstract keywords only do the following:

  • It prevents the class itself to be instantiated directly. It can only be instantiated by instantiating an inherited class. This does not change the behavior of initialization compared to a not abstract base class;

  • It allows you to have abstract methods, properties and events in the class.

If you e.g. do not have abstract methods, properties or events, exactly the same result can be accomplished by making the constructor of a class protected (like you did). This also prevents the class to be instantiated directly. The behavior does not change however compared to an abstract class.

The primary difference then becomes the ability to declare methods, properties and events as abstract which you can only do when the class is marked abstract.

like image 38
Pieter van Ginkel Avatar answered Nov 16 '22 06:11

Pieter van Ginkel


Having constructor in abstract class can be useful at times. This question is a duplicate, and dealt great deal in an related post. Even though it specifically reference JAVA, conceptually it applies to C# also.

Can an abstract class have a constructor?

like image 1
kuriouscoder Avatar answered Nov 16 '22 06:11

kuriouscoder