Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Inheritance in C# - Tutorial

Introduction to Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and behaviors from another class. The class that inherits is called the derived class, and the class being inherited from is called the base class. Inheritance promotes code reusability and establishes a natural hierarchy between classes.

Basic Syntax of Inheritance

In C#, inheritance is implemented using the : symbol. Below is a simple example demonstrating the syntax:

class BaseClass
{
    public int baseNumber;
}

class DerivedClass : BaseClass
{
    public int derivedNumber;
}

In this example, DerivedClass inherits from BaseClass. This means that DerivedClass contains all members of BaseClass.

Access Modifiers and Inheritance

Access modifiers determine the accessibility of members within a class. The main access modifiers are:

  • public: Accessible from any code.
  • private: Accessible only within the same class.
  • protected: Accessible within the same class and any derived class.

Here's an example to illustrate:

class BaseClass
{
    public int publicNumber;
    private int privateNumber;
    protected int protectedNumber;
}

class DerivedClass : BaseClass
{
    public void DisplayNumbers()
    {
        Console.WriteLine(publicNumber);     // Accessible
        // Console.WriteLine(privateNumber); // Not accessible
        Console.WriteLine(protectedNumber);  // Accessible
    }
}

Method Overriding

Method overriding allows a derived class to provide a specific implementation of a method that is already defined in its base class. This is done using the override keyword. The method in the base class must be marked with the virtual keyword.

class BaseClass
{
    public virtual void Display()
    {
        Console.WriteLine("Display from BaseClass");
    }
}

class DerivedClass : BaseClass
{
    public override void Display()
    {
        Console.WriteLine("Display from DerivedClass");
    }
}

When Display is called on an instance of DerivedClass, the overridden method in the derived class will be executed.

Base Keyword

The base keyword is used to access members of the base class from within a derived class. This can be useful for calling a base class constructor or invoking a base class method.

class BaseClass
{
    public BaseClass()
    {
        Console.WriteLine("BaseClass Constructor");
    }

    public virtual void Display()
    {
        Console.WriteLine("Display from BaseClass");
    }
}

class DerivedClass : BaseClass
{
    public DerivedClass() : base()
    {
        Console.WriteLine("DerivedClass Constructor");
    }

    public override void Display()
    {
        base.Display();
        Console.WriteLine("Display from DerivedClass");
    }
}

In this example, the base class constructor is called before the derived class constructor, and the base class Display method is called from the overridden Display method in the derived class.

Sealed Classes and Methods

The sealed keyword is used to prevent a class from being inherited or to prevent a method from being overridden. This can be useful for security and optimization purposes.

sealed class SealedClass
{
    public void Display()
    {
        Console.WriteLine("Display from SealedClass");
    }
}

// class DerivedClass : SealedClass // Not allowed
// {
// }

class BaseClass
{
    public virtual void Display()
    {
        Console.WriteLine("Display from BaseClass");
    }
}

class DerivedClass : BaseClass
{
    public sealed override void Display()
    {
        Console.WriteLine("Display from DerivedClass");
    }
}

// class FurtherDerivedClass : DerivedClass
// {
//     public override void Display() // Not allowed
//     {
//     }
// }

Conclusion

Inheritance is a powerful feature in C# that promotes code reusability and allows for the creation of a logical hierarchy between classes. Proper understanding of access modifiers, method overriding, and the use of the base and sealed keywords are essential for effective use of inheritance in your C# programs.