C# Sealed Keyword

In c#, sealed is a keyword used to stop inheriting the particular class from other classes. We can also prevent overriding the particular properties or methods based on our requirements.

 

Generally, when we create a particular class we can inherit all the properties and methods in any class. If you want to restrict access to a defined class and its members, then by using a sealed keyword, we can prevent other classes from inheriting the defined class.

C# Sealed Class

In c#, a sealed class can define by using a sealed keyword. As discussed, when we define a class with the sealed keyword, then we don’t have a chance to inherit that particular class.

 

The following are the various ways of defining sealed classes in the c# programming language.

 

sealed class Test
{
public string Name;
public string Location;
}
public sealed class Test1
{
public int Age;
}
sealed public class Test2
{
// Implementation
}

If you observe the above code snippet, we defined various classes with a sealed keyword to ensure that the defined classes are not inheritable to any class. In c#, we can use a sealed keyword before or after the access modifier to define sealed classes.

C# Sealed Class Example

Following is the example of using a sealed keyword to define sealed classes in the c# programming language.

 

using System;

namespace Tutlane
{
    // Base Class
    sealed class Users
    {
       public string name = "Suresh Dasari";
       public string location = "Hyderabad";
       public void GetInfo()
       {
          Console.WriteLine("Name: {0}", name);
          Console.WriteLine("Location: {0}", location);
       }
    }
    // Derived Class
    public class Details : Users
    {
       public int age = 32;
       public void GetAge()
       {
          Console.WriteLine("Age: {0}", age);
       }
    }
    class Program
    {
       static void Main(string[] args)
       {
          Details d = new Details();
          d.GetAge();
          d.GetInfo();
          Console.WriteLine("\nPress Enter Key to Exit..");
          Console.ReadLine();
       }
    }
}

If you observe the above example, we defined a class “Users” with a sealed keyword, and we are trying to inherit the properties from the Users class using the Details class.

 

When you execute the above c# program, we will get the result as shown below.

 

C# Sealed Classes Example Result

 

If you observe the above result, we are getting compile-time errors because we tried to inherit sealed class properties in another class.

C# Sealed Method or Property

In c#, we can also use the sealed keyword on a method or property that overrides a virtual method or property in a base class to allow other classes to derive from the base class and prevent them from overriding specific virtual methods or properties.

 

Following is the example of using a sealed keyword on a method that overrides a virtual method in a base class.

 

using System;

namespace Tutlane
{
    public class A
    {
       public virtual void GetInfo()
       {
          Console.WriteLine("Base Class A Method");
       }
       public virtual void Test()
       {
          Console.WriteLine("Base Class A Test Method");
       }
    }
    public class B: A
    {
       public sealed override void GetInfo()
       {
           Console.WriteLine("Derived Class B Method");
       }
       public override void Test()
       {
           Console.WriteLine("Derived Class B Test Method");
       }
    }
    public class C: B
    {
       // Compile time error
       public override void GetInfo()
       { 
           Console.WriteLine("Age: {0}", base.age);
       }
       public override void Test()
       {
           Console.WriteLine("Derived Class C Test Method");
       }
    }
    class Program
    {
        static void Main(string[] args)
        {
           C c = new C();
           c.GetInfo();
           c.Test();
           Console.WriteLine("\nPress Enter Key to Exit..");
           Console.ReadLine();
        }
    }
}

If you observe the above example, we used a sealed keyword on the method (GetInfo) that overrides a base class virtual method. In the above example, class C is inherited from class B, but class C cannot override a virtual method GetInfo that is declared in class A because that method is sealed in class C.

 

When you execute the above c# program, you will get a result, as shown below.

 

C# Sealed Method or Property Example Result

 

This is how you can use the sealed keyword on a method or property that overrides a base class virtual method or property.

 

Instead of using sealed keyword, we can prevent derived classes to override base class methods or properties by not declaring them as virtual.

C# Sealed Keyword Features

The following are the important points which we need to remember about the sealed keyword in the c# programming language.

 

  • In c#, to apply a sealed keyword on a method or property, it must always use with override.
  • In c#, we should not use an abstract modifier with a sealed class because an abstract class must be inherited by a class that provides an implementation of the abstract methods or properties.
  • In c#, the local variables cannot be sealed.
  • In c#, structs are implicitly sealed; they cannot be inherited.