Blog for Junior Developers C#/.NET

Thursday, July 11, 2024

Since I have recently had a lot of inquiries about topics for beginner programmers, I decided to discuss issues that every person who wants to become a .NET programmer must know. First, let me talk about what inheritance is in object-oriented programming. I will try to explain it with simple examples in C#.

what-is-inheritance-in-object-oriented-programming.jpg

What is inheritance?


Inheritance is one of the 4 basic paradigms of object-oriented programming. It is a type of relationship between two classes that allows one of them to inherit the code of the other. Thanks to it, you can build a hierarchy between classes, that is, an object can take over the methods and properties of another object. Most often, we use inheritance to create a more specialized class. That is, if we need to create a class that has many of the same properties and methods, but also some additional ones, that is, is more specialized than the parent class, then it is worth using inheritance between the two classes. We can inherit only one class and only members with the appropriate access modifier, but there will be more about this in subsequent articles.


Nomenclature


In connection with inheritance, you will often encounter terms such as derived type and subtype, which mean a type that inherits members of a more general type. And also in terms of base type, supertype, that is, a type that is more general, from which a derived type inherits. That is, if the Car class inherits from the Vehicle class, then the Car class is a derived type (subtype) of the Vehicle class. And the Vehicle class is the base type (supertype) of the Car class. Every car is a vehicle, but not every vehicle is a car.


C# example


As you have noticed in my previous articles, I like to explain a concept best by example. So let's move on to the first one. We have this code that shows us simple inheritance:

public class Vehicle
{
    public string LicensePlate { get; set; }

    public void Start()
    {
        Console.WriteLine("Vehicle started.");
    }
}

public class Car : Vehicle
{
    public void Refuel()
    {
        Console.WriteLine("Car refuel.");
    }
}

public class Program
{
    static void Main(string[] args)
    {
        Car car = new Car();
        car.LicensePlate = "ABC 11XZ";
        car.Refuel();
        car.Start();
    }
}


 As you can see we have 2 classes here. Vehicle class and car class. This entry:

public class Car : Vehicle


 It means that the Car class inherits from the Vehicle class. That is, after the class name we put a colon and the name of the class from which we want to inherit. Also, our parent type, Vehicle, has a LicensePlate property and a Start method. Due to the fact that the Car class inherits from this class, it has access to these 2 components and also has 1 additional Refuel method. As you can see in the Main method in our program, we used all these 3 components. Thanks to inheritance, we don't have to define these properties and methods again. We don't need to duplicate the code. Of course, inheritance does not work the other way round, i.e. an object of the Vehicle class will not have access to the Refuel method:

public class Program
{
    static void Main(string[] args)
    {
        Car car = new Car();
        car.LicensePlate = "ABC 11XZ";
        car.Refuel();
        car.Start();

        Vehicle vehicle = new Vehicle();
        vehicle.LicensePlate = "ABC 22XZ";
        vehicle.Refuel();//Compilation error!!
        vehicle.Start();
    }
}


Constructors


If you create a new object of a derived class, the constructor of the base class is always called first. Meaning:

public class Vehicle
{
    public string LicensePlate { get; set; }

    public Vehicle()
    {
        Console.WriteLine("Constructor Vehicle.");
    }

    public void Start()
    {
        Console.WriteLine("Vehicle started.");
    }
}

public class Car : Vehicle
{
    public Car()
    {
        Console.WriteLine("Constructor Car.");
    }

    public void Refuel()
    {
        Console.WriteLine("Car refuel.");
    }
}

public class Program
{
    static void Main(string[] args)
    {
        Car car = new Car();
        car.LicensePlate = "ABC 11XZ";
        car.Refuel();
        car.Start();
    }
}

//OUTPUT:
//Constructor Vehicle.
//Constructor Car.
//Car refuel.
//Vehicle started.


Moreover, if there is only 1 constructor in the base class and it has a parameter, then in the derived class we must pass the value to this parameter using the base keyword. If we do not do this, the compiler will report an error:

public class Vehicle
{
    public string LicensePlate { get; set; }

    public Vehicle(string licensePlate)
    {
        Console.WriteLine("Konstruktor Vehicle.");
        LicensePlate = licensePlate;
    }


    public void Start()
    {
        Console.WriteLine("Vehicle started.");
    }
}

public class Car : Vehicle
{
    public Car() : base("AA 123")
    {
        Console.WriteLine("Konstruktor Car.");
    }

    public Car(string licensePlate) : base(licensePlate)
    {
        Console.WriteLine("Konstruktor Car.");
    }

    public void Refuel()
    {
        Console.WriteLine("Car refuel.");
    }
}

public class Program
{
    static void Main(string[] args)
    {
        Car car1 = new Car("ABC 11XZ");
        car1.Refuel();
        car1.Start();

        Car car2 = new Car();
        car2.Refuel();
        car2.Start();
    }
}



Classes closed


If we want to close a class to inheritance, we should use the sealed keyword. A class marked with this attribute will never be a parent class of any other type, and you cannot inherit from it:

public sealed class Vehicle
{
    public string LicensePlate { get; set; }

    public void Start()
    {
        Console.WriteLine("Vehicle started.");
    }
}

public class Car : Vehicle //Compilation error!!!
{
    public void Refuel()
    {
        Console.WriteLine("Car refuel.");
    }
}



SUMMARY:


Inheritance is an extremely important issue when it comes to object-oriented programming. Today I tried to provide you with the most important information related to inheritance. In the next articles I will show you a few more details, so it is important that you understand the basics, otherwise you may have problems understanding subsequent issues. If you have any further questions about this article, please leave a comment. See you in the next article, where we will discuss another very important issue related to object-oriented programming.

Author of the article:
Kazimierz Szpin

KAZIMIERZ SZPIN
Software Developer C#/.NET, Freelancer. Specializes in ASP.NET Core, ASP.NET MVC, ASP.NET Web API, Blazor, WPF and Windows Forms.
Author of the blog CodeWithKazik.com

Previous article - How to Get Rid of External Dependencies in Unit Tests? Introduction to Mocking Data in C#
Next article - What is Polymorphism in Object Oriented Programming?
Comments (1)
Kazimierz Szpin
KAZIMIERZ SZPIN, poniedziałek, 15 lipca 2024 11:03
What do you think about this article? I'm curious about your opinion.
Dodaj komentarz

Search engine

© Copyright 2024 CodeWithKazik.com. All rights reserved. Privacy policy.
Design by Code With Kazik