Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it good design practice to only have a parameterless base class constructor?

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) { }
...}
like image 476
T. Webster Avatar asked Nov 30 '22 10:11

T. Webster


2 Answers

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.

like image 58
Joel Martinez Avatar answered Dec 06 '22 02:12

Joel Martinez


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.

like image 40
JonH Avatar answered Dec 06 '22 02:12

JonH