C# | Inheritance in Constructors - GeeksforGeeks

23 Jan, 2019
In C#, both the base class and the derived class can have their own constructor. The constructor of a base class used to instantiate the objects of the base class and the constructor of the derived class used to instantiate the object of the derived class. In inheritance, the derived class inherits all the members(fields, methods) of the base class, but derived class cannot inherit the constructor of the base class because constructors are not the members of the class. Instead of inheriting constructors by the derived class, it is only allowed to invoke the constructor of base class.
In C#, when we are working with the constructor in inheritance there are two different cases arise as follows:
Case 1: In this case, only derived class contains a constructor. So the objects of the derived class are instantiated by that constructor and the objects of the base class are instantiated automatically by the default constructor.
Example:
// C# program to illustrate the// concept of inheritance in the// constructor when the derived// class contains a constructorusing System;  // Class Tank to give the// dimension of the tankclass Tank {      double t_radius;    double t_height;      // Properties for Radius and Height    public double Radius    {        get {                return t_radius;             }          set {               t_radius = value < 0 ? -value : value;            }    }      public double Height    {        get {                return t_height;             }          set {               t_height = value < 0 ? -value : value;             }    }      // Display the dimension of tanks    public void DisplayDimension()    {        Console.WriteLine("The radius of tank is :" + Radius                  + " and the height of tank is :" + Height);    }}  // A derived class AreaOfTank // inheriting Tank Classclass AreaOfTank : Tank {      string Color;      // Constructor    public AreaOfTank(string c, double r, double h)    {          // from base class        Radius = r;        Height = h;          // from derived class        Color = c;    }      // Return area of tank    public double Area()    {        return 2 * 3.14 * Radius * Height;    }      // Display the color of tank    public void DisplayColor()    {        Console.WriteLine("The Color of tank is "                                         + Color);    }}  // Driver Classclass GFG {      // Main Method    static void Main()    {          // Create and initialize the        // object of AreaOfTank        AreaOfTank t1 = new AreaOfTank("Green", 6.0, 12.0);        t1.DisplayColor();        t1.DisplayDimension();        Console.WriteLine("Area is " + t1.Area());    }}
The Color of tank is Green
The radius of tank is :6 and the height of tank is :12
Area is 452.16

Explanation: In the above example Tank is the base class and AreaOfTank is the derived class. Tank class provides the dimensions of the tank and AreaOfTank provides the color and the area of the tank. And Tank class does not contain any constructor so the default constructor is used to instantiate the object of class and AreaOfTank class contains AreaOfTank() constructor which instantiate the object of class.
Case 2: In this case, both the base class and derived class has their own constructors, so the process is complicated because the constructors of both classes must be executed. To overcome this situation C# provide a keyword known as a base keyword. With the help of base keyword, the derived class can call the constructor which is defined in its base class.
Note: Any form of the constructor defined in the base class can be called by the base keyword, but only that constructor executes that matches the arguments.
Syntax:
derived-constructor(parameter-list) : base(argument-list)
{
   // body of constructor 
}

Here, argument-list contains arguments that are required by the constructor of the base class.
Example:
// C# program to illustrate the concept of // inheritance in constructors when both // the base class and derived class // their own constructorsusing System;  // Class Tank to give the // dimension of the tankclass Tank {      double t_radius;    double t_height;      // Constructor for Tank    public Tank(double r, double h)    {        Radius = r;        Height = h;    }      // Properties for Radius    // and Height    public double Radius    {        get {                return t_radius;             }          set {                t_radius = value < 0 ? -value : value;             }    }      public double Height    {        get {                return t_height;              }          set {                 t_height = value < 0 ? -value : value;             }    }      // Display the dimension of tanks    public void DisplayDimension()    {        Console.WriteLine("The radius of tank is :" + Radius                  + " and the height of tank is :" + Height);    }}  // AreaOfTank is derived class // which is inheriting the Tank classclass AreaOfTank : Tank {      string Color;      // Call the Constructor of the     // base class, i.e Tank    // Using base keyword    public AreaOfTank(string c, double r,                   double h) : base(r, h)    {        Color = c;    }      // Return area of tank    public double Area()    {        return 2 * 3.14 * Radius * Height;    }      // Display the color of tank    public void DisplayColor()    {        Console.WriteLine("The Color of tank is " + Color);    }}  // Driver Classclass GFG {      // Main Method    static void Main()    {        // Create and initialize the         // object of AreaOfTank        AreaOfTank t1 = new AreaOfTank("Brown", 4.0, 8.0);        t1.DisplayColor();        t1.DisplayDimension();        Console.WriteLine("Area is " + t1.Area());    }}
The Color of tank is Brown
The radius of tank is :4 and the height of tank is :8
Area is 200.96

Explanation: In the above example, Tank is the base class and AreaOfTank is the derived class. The Tank class describes the dimension of the tank and AreaOfTank describe the color and the area of the tank. Both the base class and the derived class have their own constructor. But we declare the constructor of AreaOfTank with a base keyword as shown here:
public AreaOfTank(string c, double r, double h) : base (r, h)
{
   Color = c; 
}

Here AreaOfTank() call base class constructor with the parameter r and h. That means Tank() constructor is called and it will initialize the value of Radius and Height in AreaOfTank(). So there is no need for AreaOfTank class to initialize these values. If AreaOfTank required an extra field, then the field should be unique from the called fields like Color.By using the base keyword, it becomes easier to initialize the objects of the base class without any conflict and it also provides an authority to call the constructor of a base class from the derived class and also save the time of re-writing the codes.
CSharp-Inheritance
CSharp-OOP
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C# | Delegates
Top 50 C# Interview Questions & Answers
Extension Method in C#
Introduction to .NET Framework
C# | Abstract Classes
C# | String.IndexOf( ) Method | Set - 1
Common Language Runtime (CLR) in C#
C# | Arrays
HashSet in C# with Examples
C# | Replace() Method