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
- 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.
How to create a Sealed Class
Step 1
- Create a class Name Common (or) keep as it as you want.
- Create the Sealed class by using “Sealed” Keyword.
- It will show the window given below in Fig 1.
Fig: 1
Syntax
Step 2 - How we can use 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.
- I created a sealed class using the “Sealed” modifier then create the three methods in sealed class and implement the methods.
- Create a new class name as test then sealed class instance create to call function.
Write the following code. Please download the “sealed class" .zip file.
Coding
- 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 MethodName(string[] args)
- {
- Common Ocommon = new Common();
-
- int Value= Ocommon.Add(10, 25);
- Console.WriteLine("Your Value is" + Value.ToString());
-
- int subValue = Ocommon.Sub(25, 10);
- Console.WriteLine("Your Value is" + subValue.ToString());
-
- int mulValue = Ocommon.Multiply(10, 2);
- Console.WriteLine("Your Value is" + mulValue.ToString());
- }
-
-
- }
- }
Step 3 - Finding the Error
- Inheritance type shows an error message.
- The error occurs if a sealed class is specified as the base class of another class.
- It will show in the Screenshot given below.
Fig: 2
Difference between static class and sealed class
Sealed class
|
Static class
|
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 class, 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.