Header add

In this article we will discuss how to create and use a sealed class in C#. In this topic we cover
>> What is sealed class in C# ?
>> How to use sealed class in c# ?
What is sealed class in C#
Sealed Class are used to restrict the inheritance feature of object-oriented programming. Once a class is defined as a sealed class, this class cannot be inherited, but we can create an instance to call the function. A Sealed class is created using the “Sealed” keyword in C#.  A sealed class can be a derived class but cannot be a base class and also cannot be an abstract class since the abstract class must provide functionality.

Some key points of sealed class are,
  • A Sealed class is created by using the "sealed" Modifier.
  • To access the members of the sealed class we can instance create of the class.
  • Sealed class is defined this class cannot be inherited.
  • The sealed class is the inheritance level of a class.
Sealed class are defined like below;
      public sealed class Calculator { 
How to use sealed class in c#
In the below example we take a C# console application and declare a sealed class named as calculator and implement some method into it and then call it into Main() method.
    namespace SealedClass  
       public sealed class Calculator {  
         public int Add(int x, int y)  
           return (x + y);  
         public int Sub(int x, int y)  
           return (x - y);  
         public int Multiply(int x, int y)  
           return (x * y);  
         public int Division(int x, int y)  
           return (x / y);  
      public class Program  
         static void Main(string[] args)  
           Calculator calc = new Calculator();  
           int add = calc.Add(15, 25);  
           Console.WriteLine("Your Value is " + add.ToString());  
           int sub = calc.Sub(25, 15);  
           Console.WriteLine("Your Value is " + sub.ToString());  
           int mul = calc.Multiply(15, 5);  
           Console.WriteLine("Your Value is " + mul.ToString());  
           int div = calc.Division(15, 5);  
           Console.WriteLine("Your Value is " + div.ToString());  
When we run the application you can see the output like this..

As per the syntax of sealed class it can not be inherited. Let's create a dummy class and try to inherit the Calculator() class

You can clearly see that sealed class can not be inherit and it shows compile time error

Why Sealed Classes are used ?

We just saw how to create and use a sealed class in C#. The main purpose of a sealed class is to take away the inheritance feature from the class users so they cannot derive a class from it. One of the best usage of sealed classes is when you have a class with static members. For example, the Pens and Brushes classes of the System.Drawing namespace.

The Pens class represents the pens with standard colors. This class has only static members. For example, Pens.Blue represents a pen with blue color. Similarly, the Brushes class represents standard brushes. The Brushes.Blue represents a brush with blue color.

So when you're designing a class library and want to restrict your classes not to be derived by developers, you may want to use sealed classes.

</> Find the Source Code in Github.com/CoreProgramm/

   In this tutorial we discussed how to use Sealed class in C#. If have any question related to this topic then give your feedback.

Post a Comment

Previous Post Next Post