C# | Inheritance in Constructors

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// concept of inheritance in the
// constructor when the derived
// class contains a constructor
using System;
  
// Class Tank to give the
// dimension of the tank
class 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 Class
class 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 Class
class 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());
    }
}

chevron_right


Output:



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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the concept of 
// inheritance in constructors when both 
// the base class and derived class 
// their own constructors
using System;
  
// Class Tank to give the 
// dimension of the tank
class 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 class
class 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 Class
class 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());
    }
}

chevron_right


Output:

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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.