Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Object Creation of Inherited Class
In C# inheritance, when creating objects of derived classes, the base class constructor is called first, followed by the derived class constructor. This ensures that the base class is properly initialized before the derived class adds its own functionality.
The derived class inherits member variables and methods from the base class. When instantiating a derived class object, you can use the base keyword to explicitly call the base class constructor and pass required parameters.
Syntax
Following is the syntax for calling a base class constructor from a derived class −
public class DerivedClass : BaseClass {
public DerivedClass(parameters) : base(parameters) {
// derived class constructor code
}
}
Following is the syntax for calling base class methods from derived class −
base.MethodName(); // calls the base class version of the method
Object Creation Process
When you create an object of a derived class, the following sequence occurs:
Using base() Constructor
Example
using System;
class Rectangle {
protected double length;
protected double width;
public Rectangle(double l, double w) {
length = l;
width = w;
Console.WriteLine("Rectangle constructor called");
}
public double GetArea() {
return length * width;
}
public virtual void Display() {
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class Tabletop : Rectangle {
private double cost;
public Tabletop(double l, double w) : base(l, w) {
Console.WriteLine("Tabletop constructor called");
}
public double GetCost() {
double cost;
cost = GetArea() * 70;
return cost;
}
public override void Display() {
base.Display();
Console.WriteLine("Cost: {0}", GetCost());
}
}
class Program {
static void Main(string[] args) {
Tabletop t = new Tabletop(3, 8);
t.Display();
}
}
The output of the above code is −
Rectangle constructor called Tabletop constructor called Length: 3 Width: 8 Area: 24 Cost: 1680
Multiple Level Inheritance
Example
using System;
class Shape {
public Shape() {
Console.WriteLine("Shape constructor");
}
}
class Rectangle : Shape {
protected double length, width;
public Rectangle(double l, double w) : base() {
length = l;
width = w;
Console.WriteLine("Rectangle constructor");
}
}
class ColoredRectangle : Rectangle {
private string color;
public ColoredRectangle(double l, double w, string c) : base(l, w) {
color = c;
Console.WriteLine("ColoredRectangle constructor");
}
public void ShowDetails() {
Console.WriteLine("Colored Rectangle: {0}", color);
Console.WriteLine("Area: {0}", length * width);
}
}
class Program {
static void Main(string[] args) {
ColoredRectangle cr = new ColoredRectangle(5, 3, "Blue");
cr.ShowDetails();
}
}
The output of the above code is −
Shape constructor Rectangle constructor ColoredRectangle constructor Colored Rectangle: Blue Area: 15
Key Points
-
Base class constructors execute before derived class constructors.
-
Use
base(parameters)to explicitly call a specific base class constructor. -
If no
base()call is specified, the parameterless base constructor is called automatically. -
Protected members in base class are accessible in derived class but not outside the class hierarchy.
Conclusion
Object creation in inherited classes follows a predictable sequence where base class constructors execute first. Using the base keyword allows precise control over base class initialization, ensuring proper inheritance chain setup and access to both base and derived class functionality.
