In base class constructors I always see a parameterless constructor, like so:
public abstract BaseClass {...
protected BaseClass() { }
...}
but is it acceptable design to include a parameter in the base class constructor?
public abstract BaseClass {...
protected BaseClass(string initObj) { }
...}
Yes, it is acceptable for a base class to require a parameterized constructor. This simply imposes a requirement that any classes which inherit must provide a value to the base constructor.
In most cases the derived classes have some form of parameterized constructors. So when those constructors are called they can still call the parameterless base constructor:
public employee(int age) : base(this)
The answer is if you need one just add one, there is nothing wrong with that. Think of a business object base class that requires some validations to say a phone number or email address. You want to ensure the derived classes get these business rules loaded into them. If you did not have the base class constructor you could not add these rules to your derived class objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With