Sealed Class and Sealed Methods in C#

Sealed Class in C#

A class from which it is not possible to create/derive a new class is known as sealed class. In simple word, we can also define the class that is declared using the sealed modifier is known as the sealed class and a sealed class cannot be inherited by any other class.

For example:

sealed class Class1 {}

class class2 : Class1{} //invalid

To make any class as sealed class we need to use the keyword sealed.

Points to Remember while working with Sealed Class
  1. A sealed class is completely opposite to an abstract class.
  2. This sealed class cannot contain abstract methods.
  3. It should be the bottom most class within the inheritance hierarchy.
  4. A sealed class can never be used as a base class.
  5. This sealed class is specially used to avoid further inheritance.
  6. The keyword sealed can be used with classes, instance methods, and properties.

NOTE: Even if a sealed class cannot be inherited we can still consume the class members from any other class by creating the object of the class.

Sealed Methods in C#

The method that is defined in a parent class, if that method cannot be overridden under a child class, we call it a sealed method.

By default, every method is a sealed method because overriding is not possible unless the method is not declared as virtual in the parent class.

If a method is declared as virtual in a class, any child class of it can have the rights to override that method.

For Example:

namespace SealedDemo
{
    class class1
    {
        public virtual void show() { }
    }
    class class2 : class1
    {
        public override void show() { }
    }
    class class3 : class2
    {
        public override void show() { }
    }
}

In the above case even if the first child is not overriding the method the second child can still override the method.

When a child class is overriding its parent class virtual methods the child class uses the sealed modifier in the method so that further overriding of the method will not be possible i.e. child classes cannot override the methods.

For example:
namespace SealedDemo
{
    class class1
    {
        public virtual void show() { }
    }
    class class2 : class1
    {
        public sealed override void show() { }
    }
    class class3 : class2
    {
        //'class3.show()': cannot override inherited member 'class2.show()' because it is sealed
        public override void show() { } //Invalid
    }
}
Let’s understand one example for better understanding.
namespace SealedDemo
{
    public class Employee
    {
        protected int Eid, Eage;
        protected string Ename, Eaddress;
        public virtual void GetEmployeeData()
        {
            Console.WriteLine("ENTER EMPLOYEE DETAILS:");
            Console.WriteLine("ENTER THE ID");
            Eid = int.Parse(Console.ReadLine());
            Console.WriteLine("ENTER THE NAME");
            Ename = Console.ReadLine();
            Console.WriteLine("ENTER THE ADDRESS");
            Eaddress = Console.ReadLine();
            Console.WriteLine("ENTER THE AGE");
            Eage = int.Parse(Console.ReadLine());
        }
        public virtual void DisplayEmployeeData()
        {
            Console.WriteLine("\nEMPLOEE DETAILS ARE:");
            Console.WriteLine("EMPLOYEE ID IS: " + Eid);
            Console.WriteLine("EMPLOYEE NAME IS: " + Ename);
            Console.WriteLine("EMPLOYEE ADDRESS IS:" + Eaddress);
            Console.WriteLine("EMPLOYEE AGE IS : " + Eage);
        }
    }

    public sealed class Manager : Employee
    {
        double Bonus, CA;
        public override void GetEmployeeData()
        {
            Console.WriteLine("ENTER MANAGER DETAILS:");
            Console.WriteLine("ENTER THE ID");
            Eid = int.Parse(Console.ReadLine());
            Console.WriteLine("ENTER THE NAME");
            Ename = Console.ReadLine();
            Console.WriteLine("ENTER THE BONUS");
            Bonus = double.Parse(Console.ReadLine());
            Console.WriteLine("ENTER THE CA");
            CA = Convert.ToDouble(Console.ReadLine());
        }
        public override void DisplayEmployeeData()
        {
            Console.WriteLine("MANAGER DETAILS ARE:");
            Console.WriteLine("EMPLOYEE ID IS: " + Eid);
            Console.WriteLine("EMPLOYEE NAME IS: " + Ename);
            Console.WriteLine("MANAGER BONUS IS: " + Bonus);
            Console.WriteLine("MANAGER CA IS : " + CA);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Manager m1 = new Manager();
            m1.GetEmployeeData();
            m1.DisplayEmployeeData();
            Console.ReadKey();
        }
    }
}

OUTPUT:

Sealed Class and Sealed Methods in C#

When should a method be declared as sealed?

If we don’t want to allow sub-classes to override the superclass method and to ensure that all sub-classes use the same superclass method logic then that method should be declared as sealed. The sealed method cannot be overridden in sub-classes violation leads to compile time error.

What is the difference between private and sealed method?

The private method is not inherited whereas sealed method is inherited but cannot be overridden. So, a private method cannot be called from sub-classes whereas sealed method can be called from sub-classes. The same private method can be defined in sub-class and it does not lead to error.

When should a class be declared as sealed?

In the below situations we must define the class as sealed

  1. If we don’t want to override all the methods of our class in sub-classes.
  2. And if we don’t want to extend our class functionality.

What are the differences between an abstract class and a sealed class?

SL NO ABSTRACT CLASS SEALED CLASS
1. A class which contains one or more abstract methods is known as an abstract class. A class from which it is not possible to derive a new class is known as sealed class.
2. The abstract class can contain abstract and non-abstract methods. The sealed class can contain non-abstract methods; it cannot contain abstract and virtual methods.
3. Creating a new class from an abstract class is compulsory to consume it. It is not possible to create a new class from a sealed class.
4. An abstract class cannot be instantiated directly; we need to create the object for its child classes to consume an abstract class. We should create an object for a sealed class to consume its members.
5. We need to use the keyword abstract to make any class as abstract. We need to use the keyword sealed to make any class as sealed.
6. An abstract class cannot be the bottom-most class within the inheritance hierarchy. The sealed class should be the bottommost class within the inheritance hierarchy.
SUMMARY:

In this article, I try to explain Sealed Class and Sealed Methods in C# with some examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

1 thought on “Sealed Class and Sealed Methods in C#”

  1. Suresh Tipperaboina

    Hi bro,

    Really am impressed with your explanation of all the concepts. Even fresher or lay man with out any knowledge also can understand the concepts of your way of explanation, because you have explained concept which is covered the below features.
    1. What is the concept.
    2. Why is this concept.
    3. How this concept will be useful in real world programming.
    4. When this concept can be used.
    5. What are all the rules,restrictions and limitations of this concept.
    6. Correlating between concept to concept.
    7. And more over explanation with practical examples and real time examples.

    Finally Hats off to you bro..

Leave a Reply

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