Sealed Class Explained In C#

Introduction

In this article, I will explain how to create and use a sealed class in C# with some examples. A Sealed class is created using the “Sealed” keyword in C#. The Sealed Class is used for the restricted inheritance feature of object-oriented programming. Once the sealed class has been defined, this class cannot be inherited, but we can create an instance to call the function. The sealed keyword is used to declare a class as sealed. A sealed class is used for the inheritance level of a class. This class cannot be inherited by any other class. 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. I have written this article focusing on students and beginners.

Key Points

  1. A Sealed class is created by using the "sealed" Modifier.
  2. To access the members of the sealed class, we can instance create of the class.
  3. A sealed class is defined as a class that cannot be inherited.
  4. The sealed class is the inheritance level of a class.

Step 1: How to Create a Sealed Class?

  1. Create a class Name Common (or) keep as it as you want.
  2. Create the Sealed class by using the “Sealed” Keyword.
  3. It will show the window given below in Fig 1.

Sealed Class Explained In C#

Fig.1

Syntax

sealed class Common
{
    // Class content goes here...
}

Step 2: How we can use a sealed class?

In this following code, create a sealed class using the “Sealed” Keyword and use it from the Common class. Then, create a new method in the same class and implement the methods. Call the class named test and run this code. We will get an error.

  1. I created a sealed class using the “Sealed” modifier, then created the three methods in the sealed class and implemented the methods.
  2. Create a new class name as a test, then seal the class instance created to call the function.

Write the following code. Please download the “sealed class" .zip file.

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SealedClassTest
{
    public sealed class Common
    {
        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 class Test
    {
        static void Main(string[] args)
        {
            Common Ocommon = new Common();
            // Add
            int Value = Ocommon.Add(10, 25);
            Console.WriteLine("Your Value is " + Value.ToString());
            // Sub
            int subValue = Ocommon.Sub(25, 10);
            Console.WriteLine("Your Value is " + subValue.ToString());
            // Multiply
            int mulValue = Ocommon.Multiply(10, 2);
            Console.WriteLine("Your Value is " + mulValue.ToString());
        }
    }
}

Step 3: Finding the Error 

  1. Inheritance type shows an error message.
  2. The error occurs if a sealed class is specified as the base class of another class.
  3. It will show in the Screenshot given below.

sealed class test

Fig. 2

Difference between Static class and Sealed class
 

Sealed class Static class
A sealed class can instance create to call function static class is we cannot instance create to call function
The sealed class contains static as well as non-static members The static class is we can access to static members only
Sealed class can't be never inherited static class is we can inherit by other static class
Sealed class is we can't use to base class static class is we can use to base class

Note.If you want more information about static classes, please refer to the previous articles.

Summary

In this article, I discussed a Sealed class in C#. I have written this article focusing on beginners and students.


Recommended Free Ebook
Similar Articles