Sealed Class in C#

Classes declared with the sealed keyword cannot be extended. That means, when you declare a base class with this keyword, you cannot use it in a derived class. In other words the class cannot be extended. This keyword is similar to that of final keyword in Java.
 
The given example SealedClass.cs is a modified version of the example of Inheritance.cs. Here we are giving the example of sealed class. It declares the base class xyz as sealed..
 
// SealedClass.cs
 
using System;
// error location
sealed class xyz
{
// Erase the Protected modifier and observe the result. 
public void Sum()
{
int num1 = 10;
int num2 = 20;
int num3 = num1+num2;
Console.WriteLine(num3);
}
}
class derivedclassSealed: xyz
{
public static void Main()
{
derivedclassSealed hd = new derivedclassSealed ();
hd.Sum();
}
}
 
When you execute the above code, you will get a compilation error indicating that the derived class cannot inherit from the sealed base class Computer.
 
To Download Sealed Class example Click here.
 
To view downloaded examples, Please Ensure that .NET Framework is installed on your system.
 
Scroll to Top