Skip to content

C# Constructor Inheritance

C# Constructor Inheritance

C# Constructor Inheritance allows derived classes to inherit and utilize the constructors of their base classes. This feature provides a more efficient way to reuse code, reduce redundancy, and promote maintainability of the codebase. In this article, we will explore the syntax, and provide real-world scenario code examples. By the way, did you know that we offer a unique online course that boosts your C# career? Check it out here!

 

Why?

In object-oriented programming, inheritance is the mechanism that enables a class to inherit properties and behaviors of another class. Constructors are special methods that are invoked during the creation of an object, and they are used to initialize the fields of the object. When a class is derived from another class, it is common to want to utilize the constructors of the base class to initialize the fields inherited from the base class. C# enables this by allowing derived classes to utilize the constructors of their base classes.

By using constructor inheritance, developers can avoid duplicating code for initializing the same fields in both the base class and the derived class. This not only reduces the amount of code that needs to be written but also improves the maintainability of the codebase and reduces the likelihood of bugs due to duplicate code.

 

Syntax

The syntax is quite simple. After defining the constructor in a derived class, add the base keyword followed by the parameter list of the constructor in the base class.

 

 

In the example above, the Person class has a constructor that takes two parameters name and age and initializes the Name and Age properties. The Employee class is derived from the Person class and declares a constructor that takes three parameters name, age, and employeeId. The Employee constructor uses the base keyword to call the constructor of the Person class with the name and age parameters.

 

Examples

The following are some examples that demonstrate the use of constructor inheritance.

Example 1: A Shape Class Hierarchy

 

In the example above, the Shape class has a constructor that takes two parameters x and y and initializes the X and Y properties. The Circle and Rectangle classes are derived from the Shape class and declare their own constructors that call the constructor of the Shape class using the base keyword. The Circle class adds a Radius property and the Rectangle class adds Width and Height properties.

 

Example 2: A Base Database Connection Class

 

In the example above, the DbConnection class is a base class for database connections and has a constructor that takes a connectionString parameter. The SqlConnection class and OracleConnection class are derived from the DbConnection class and declare their own constructors that call the constructor of the DbConnection class using the base keyword. The SqlConnection class and OracleConnection class also override the Open and Close methods to provide database-specific implementations.

 

Conclusion

Calling base constructors promotes code reuse, reduces code redundancy and helps developers to produce maintainable code that is easier to understand and extend. If you want to skyrocket your C# career, check out our powerful ASP.NET full-stack web development course that also covers test-driven development.

Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide