开发者

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
{
    pub开发者_运维百科lic TaskScheduler()
        : base()
    {

    }
}


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.


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.


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?


Constructors on abstract types can be called only by derived types. Because public constructors create instances of a type, and you cannot create instances of an abstract type, an abstract type that has a public constructor is incorrectly designed. CA1012: Abstract types should not have constructors

Fix the violation by changing the accessibility of the constructor from public to protected.

Example:

namespace Sample  
{        
    public abstract class Book      
    {          
        protected Book()           
        {          
        }      
    } 
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜