Skip to main content
C#

Supercharge Your C# Classes - Mastering the Sealed Keyword for Better Code Control

Christian Schou

In the world of C# programming, inheritance is a fundamental idea that enables programmers to build on existing classes to create hierarchies of classes that promote code reuse and organization. However, as the project develops, this power has the potential to bring about unintended complications and problems. The "Sealed" keyword can help in this situation.

Introduction to inheritance and its role in C# programming

A crucial concept in object-oriented programming is inheritance, which allows a class (the derived class) to take on traits and behaviors from another class (the base class). This supports the DRY (Don't Repeat Yourself) principle by allowing the derived class to reuse code and increase the functionality of the base class.

Introducing the "Sealed" keyword and its significance

In C#, the "Sealed" keyword can be used as a modifier on classes or methods. It prevents further inheritance when applied to a class, making it final and non-extendable. In other words, no other class may use a sealed class as its base class.

Developers have access to a potent tool in the "Sealed" keyword that gives them control over class hierarchies and facilitates the development of robust and secure codebases.

In this blog post, I'll go in-depth on the "Sealed" keyword. By the end, you'll know when and why to use it, be aware of its advantages, and have seen examples of how it can improve the performance and maintainability of code.

A quick introduction to Inheritance in C#

A key concept in object-oriented programming (OOP) is inheritance, which enables a class to take on traits and characteristics from another class. The foundational class is referred to as the base class, and the class that derives from it is referred to as the derived class. With the derived class being a more specialized version of the base class, this creates a hierarchical relationship between classes.

The advantages of inheritance are numerous, but here are a few I would like to highlight.

  • Code Reusability - By enabling developers to define common functionality in a base class and reuse it across multiple derived classes, inheritance promotes code reuse. This reduces code duplication and enhances maintenance.
  • Polymorphism - Inheritance makes it possible for objects of various derived classes to be treated as belonging to the base class, enabling dynamic method invocation and flexibility in code design.
  • Organized Class Hierarchy - By arranging classes in a hierarchy, code is made more organized and simpler to read and understand. The core functionality is contained in the base class, and additional features are added by derived classes.

The concept of base classes and derived classes

In C#, classes are defined using the class keyword. To create a derived class, you use a colon followed by the name of the base class after the derived class's name. Here is an example for you.

// Base class
class Animal
{
    public void Eat()
    {
        Console.WriteLine("The animal is eating.");
    }
}

// Derived class
class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("Woof! Woof!");
    }
}

What happens in the code above? 🤔

  • The Animal class acts as the base class.
  • The Dog class is a derived class that inherits from Animal.
  • The Dog class has access to the Eat() method defined in the Animal class.

How C# supports inheritance through class hierarchies

A class in C# can only derive from one base class because it supports single inheritance. This guarantees a distinct and unmistakable relationship between each class in the hierarchy. To achieve a form of multiple inheritance through interface inheritance, C# also permits the implementation of multiple interfaces.

The Sealed Keyword Explained

The "Sealed" keyword is a modifier that can be used on methods or classes in C#. It prevents inheritance from a class when used with it, rendering the class final or non-extendable.

🤓
A sealed class cannot serve as the base class for other classes, in other words. The "Sealed" keyword restricts derived classes from overriding a method when it is applied to a method.

How the "Sealed" keyword affects class inheritance

To illustrate how the "Sealed" keyword works, I have made a simple example for you.