• Javascript
  • Python
  • Go
Tags: c# constructor

Calling Overridden and Base Constructors in C#

In C#, constructors are used to initialize the state of an object when it is created. They are special methods that have the same name as th...

In C#, constructors are used to initialize the state of an object when it is created. They are special methods that have the same name as the class and are called automatically when an instance of the class is created. In some cases, a class may have multiple constructors, and they can be overridden or called from a base constructor. In this article, we will explore how to call overridden and base constructors in C#.

To understand the concept of overridden and base constructors, let's first take a look at what they mean. An overridden constructor is a constructor that has the same name as a constructor in the base class but has a different implementation. This allows a derived class to provide its own initialization logic while still utilizing the base class's constructor. On the other hand, a base constructor is the constructor in the base class that is called when a derived class is instantiated.

Let's consider a simple example to demonstrate how overridden and base constructors work. Suppose we have a base class called "Vehicle" with a constructor that takes in a string parameter for the vehicle's model. We also have a derived class called "Car" that inherits from the "Vehicle" class and has its own constructor that takes in an additional int parameter for the car's year.

```

public class Vehicle

{

public string Model { get; set; }

public Vehicle(string model)

{

this.Model = model;

}

}

public class Car : Vehicle

{

public int Year { get; set; }

public Car(string model, int year) : base(model)

{

this.Year = year;

}

}

```

In the above example, the "Car" class's constructor calls the base class's constructor using the "base" keyword and passing in the model parameter. This ensures that the base class's initialization logic is still executed while also allowing the derived class to add its own logic for the year parameter.

Now let's see how we can call these constructors in our code. To create an instance of the "Car" class, we can use the "new" keyword and pass in the required parameters.

```

Car myCar = new Car("Honda Civic", 2020);

```

This will call the "Car" class's constructor, which in turn calls the base class's constructor, passing in the model parameter "Honda Civic". The "Year" property will also be initialized with the value 2020.

But what if we want to call the base constructor from within the overridden constructor in the derived class? This can be achieved using the "base" keyword again, but this time within the derived class's constructor.

```

public class Car : Vehicle

{

public int Year { get; set; }

public Car(string model, int year) : base(model)

{

this.Year = year;

}

public Car(string model) : base(model)

{

//do some additional logic

base.Model = model + " DX"; //calling base constructor and adding " DX" to the model

}

}

```

In the above example, we have added another constructor to the "Car" class that takes in only the model parameter. We can see that the base constructor is called using the "base" keyword, and we can also perform additional logic in the derived class's constructor.

In conclusion, calling overridden and base constructors in C# allows for flexibility in class initialization and enables derived classes to add their own

Related Articles

Base Constructor Call in C#

HTML (HyperText Markup Language) is the backbone of any web page, providing structure and formatting for all the content. But did you know t...