The post C# Introduction lists all of the C# series of posts we have at this site.
There are two things you need to know about constructors when it come to inheritance. As Mosh says in his video course,
- During the instantiation of an object, the base (parent) class constructors are always executed first.
- Base class constructors are not inherited, so in a derived class you need to re-define your constructors.
Vehicle and Car
Vehicle is the parent of Car. Car derives from Vehicle. We want to use this example to illustrate constructors and inheritance. We will create our own classes: Vehicle and Car. Car is a type of vehicle. As a side note, we would use composition if we wanted to design an engine class because engine is not a type of vehicle, but vehicles have an engine. This example is based on a Udemy.com course on C# by Mosh Hamedani.
We end up using the base keyword to pass an argument from the child constructor to the base (parent) constructor. Let’s use an example to see this in action. From the Intermediate C# Udemy.com course, using Mosh’s example, we have a Vehicle class and a Car class. Car inherits from Vehicle.
using System; namespace ConstructorsInheritanceMosh { public class Vehicle { private readonly string _registrationNumber; // private means not accessible inside Car class // need to use base to pass string from car to vehicle public Vehicle(string registrationNumber) { _registrationNumber = registrationNumber; Console.WriteLine("vehicle is being initialized..."); } } public class Car : Vehicle { public Car(string registrationNumber) : base(registrationNumber) { // base is like a method call to the Vehicle's constructor Console.WriteLine("car is being initialized..."); } } class Program { static void Main(string[] args) { var car = new Car("VIN123"); } } }
There are a few things happening here in the code below. We have added some more code. We have default constructors for both Vehicle and Car.
using System; namespace ConstructorsInheritanceMosh { public class Vehicle { private readonly string _registrationNumber; // private means not accessible inside Car class // need to use base to pass string from car to vehicle public Vehicle(string registrationNumber) { _registrationNumber = registrationNumber; Console.WriteLine($"vehicle is being initialized with registration: {registrationNumber}"); } public Vehicle() { Console.WriteLine("vehicle is being initialized..."); } } public class Car : Vehicle { public Car(string registrationNumber) : base(registrationNumber) { // base is like a method call to the Vehicle's constructor Console.WriteLine($"car is being initialized with registration: {registrationNumber}"); } public Car() { Console.WriteLine("car is being initialized..."); } } class Program { static void Main(string[] args) { // you get 6 lines of output here. (plus press any key...) var car1 = new Car(); // vehicle is being initialized... (and car!) var car2 = new Car(""); // vehicle is being initialized with registration: (and car!) var car3 = new Car("VIN1234"); // vehicle is being initialized with registration: VIN1234 (and car!) } } }