C# Sealed Keyword

In c#, sealed is a keyword which is used to stop inheriting the particular class from other classes and 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. In case, if you want to restrict an access of defined class and its members, then by using sealed keyword we can prevent other classes from inheriting the defined class.

C# Sealed Class

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

 

Following are the various ways of defining a sealed classes in 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 above code snippet, we defined a various classes with sealed keyword to make sure that the defined classes are not inheritable to any class. In c#, we can use sealed keyword before or after the access modifier to define a sealed classes.

C# Sealed Class Example

Following is the example of using a sealed keyword to define a sealed classes in 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

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

 

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

 

C# Sealed Classes Example Result

 

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

C# Sealed Method or Property

In c#, we can also use 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 base class but to prevent them from overriding a 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we used a sealed keyword on method (GetInfo) that overrides a base class virtual method. In above example, class C is inherits 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 we execute above c# program, we will get a result like as shown below.

 

C# Sealed Method or Property Example Result

 

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

 

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

C# Sealed Keyword Features

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

 

  • In c#, to apply a sealed keyword on method or property, it must always be used with override.
  • In c#, we should not use abstract modifier with 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.