Inheritance in C# Classes

Introduction

Hello again! In this part of our C# Class Basics Revision, we delve into inheritance in object-oriented programming (OOP) with C#. Inheritance allows us to share code across classes, thus improving readability and efficiency.

In this lesson, we'll clarify attribute and method inheritance in C# using practical examples. Our lesson's blueprint includes defining inheritance, examining attribute inheritance, exploring method inheritance, and decoding the base keyword in C#. Ready? Let's get started!

Defining Inheritance

Inheritance involves creating a child class that inherits details from a parent class. In C#, we often find scenarios where classes share common attributes or methods, which makes inheritance highly useful.

The : symbol is used to set up inheritance, allowing one class to inherit properties and methods from another class. Here's an example featuring a parent class named Vehicle and a child class named Car:

// Define the parent class 'Vehicle'
class Vehicle {
    // Initialize the Vehicle with color and brand attributes
    public string Color { get; }
    public string Brand { get; }

    public Vehicle(string color, string brand) {
        Color = color;
        Brand = brand;
    }
}

// Define the child class 'Car', inheriting from 'Vehicle'
class Car : Vehicle {
    public int Doors { get; }

    public Car(string color, string brand, int doors) : base(color, brand) {
        Doors = doors;
    }
}

In the Car class, the base() constructor inside its constructor calls the Vehicle class's constructor, enabling the inherited properties to be initialized correctly. The : symbol signifies that Car is a subclass of Vehicle.

Inheritance types, such as Single, Multiple, Multilevel, and Hierarchical, in C# cater to different needs. However, our focus in this lesson is primarily on single inheritance, where one parent class feeds one child class.

Attribute Inheritance

Attribute inheritance allows a child class to inherit the attributes of a parent class, with the exception of private fields. Private fields (declared with the private keyword) are not accessible in child classes.

Consider this example featuring a parent class named Artist, and a child class named Musician:

using System;

public class Artist {
    public string Name { get; }

    public Artist(string name) {
        Name = name;
    }
}

public class Musician : Artist {
    public string Instrument { get; }

    public Musician(string name, string instrument) : base(name) {
        Instrument = instrument;
    }
}

public class Program {
    public static void Main(string[] args) {
        var john = new Musician("John Lennon", "Guitar");
        Console.WriteLine(john.Name); // Output: John Lennon
        Console.WriteLine(john.Instrument); // Output: Guitar
    }
}

However, if the Name attribute in the Artist class were private, it wouldn't be accessible in the Musician class:

using System;

public class Artist {
    private string _name;

    public Artist(string name) {
        _name = name;
    }

    public string GetName() {
        return _name;
    }
}

public class Musician : Artist {
    public string Instrument { get; }

    public Musician(string name, string instrument) : base(name) {
        Instrument = instrument;
    }
}

public class Program {
    public static void Main(string[] args) {
        var john = new Musician("John Lennon", "Guitar");
        Console.WriteLine(john.GetName()); // Output: John Lennon
        Console.WriteLine(john.Instrument); // Output: Guitar
    }
}

The Musician class inherits the _name attribute from the Artist class and also has its own unique attribute, Instrument, but it accesses the _name attribute via a getter method since _name is private.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal