C# Keywords Tutorial Part 73: sealed

C# Keywords Tutorial Part 73: sealed

The “sealed” keyword in C# is used to limit a class’s or a member’s ability to inherit from another class. A class or member thereof that has the designation “sealed” cannot be inherited by another class or overridden by a derived class. We will examine the C# term “sealed” and instances of its usage in this blog article.

Classes, methods, and properties can all use the “sealed” keyword. A class that has the designation “sealed” cannot be inherited by any other class. Similar to this, any derived class cannot override a method or property that is declared as sealed. Let’s examine the “sealed” keyword’s use with some samples from the code.

Sealed Class

Let’s start with an example of a sealed class. Suppose we have a base class called “Vehicle” that has a method called “Drive.” We want to restrict the inheritance of the “Vehicle” class, so we mark it as sealed using the “sealed” keyword.

sealed class Vehicle
{
    public void Drive()
    {
        Console.WriteLine("Driving the vehicle.");
    }
}

class Car : Vehicle // This will give an error since Vehicle is marked as sealed
{
    public void Start()
    {
        Console.WriteLine("Starting the car.");
    }
}

In the above code, the “Vehicle” class is marked as sealed, so we cannot inherit it in the “Car” class. If we try to inherit it, the compiler will give us an error.

Sealed Method

Now, let’s explore an example of a sealed method. Suppose we have a base class called “Animal” that has a method called “MakeSound.” We want to restrict the override of the “MakeSound” method in any derived class, so we mark it as sealed using the “sealed” keyword.

class Animal
{
    public sealed void MakeSound()
    {
        Console.WriteLine("Making sound.");
    }
}

class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("Barking.");
    }

    public override void MakeSound() // This will give an error since MakeSound is marked as sealed
    {
        Console.WriteLine("Making sound."); 
    }
}

In the above code, the “MakeSound” method of the “Animal” class is marked as sealed. So, we cannot override it in the “Dog” class. If we try to override it, the compiler will give us an error.

Sealed Property

Finally, let’s explore an example of a sealed property. Suppose we have a base class called “Person” that has a property called “Name.” We want to restrict the override of the “Name” property in any derived class, so we mark it as sealed using the “sealed” keyword.

class Person
{
    public virtual string Name { get; set; }
}

class Employee : Person
{
    public sealed override string Name
    {
        get { return "Employee"; }
        set { }
    }
}

class Manager : Employee
{
    public override string Name // This will give an error since Name is marked as sealed
    {
        get { return "Manager"; }
        set { }
    }
}

In the above code, the “Name” property of the “Employee” class is marked as sealed. So, we cannot override it in the “Manager” class. If we try to override it, the compiler will give us an error.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *