Sealed classes are used to restrict the users from inheriting the class. A class can be sealed by using the sealed keyword. The keyword tells the compiler that the class is sealed, and therefore, cannot be extended. No class can be derived from a sealed class. Syntax for using sealed class is attached below:
sealed class class_name
{
// data members
// methods
.
.
.
}
A method can also be sealed, and in that case, the method cannot be overridden. However, a method can be sealed in the classes in which they have been inherited. If you want to declare a method as sealed, then it has to be declared as virtual in its base class.
Example 1: Create a Sealed Class that can be used in Geeks Class
C#
// Using Sealed Class
using System;
// Sealed class
sealed class SealedClass
{
// Calling Function
public int Add(int a, int b)
{
return a + b;
}
}
class Geeks
{
// Main Method
static void Main(string[] args)
{
// Creating an object of Sealed Class
SealedClass slc = new SealedClass();
// Performing Addition operation
int total = slc.Add(6, 4);
Console.WriteLine("Total = " + total.ToString());
}
}
Example 2: Trying to inherit a class from a sealed class then an error will be produced stating that " It cannot be derived from a Sealed class"
C#
// Demonstration of restrictions of Sealed Class
using System;
class Bird
{
}
// Creating a sealed class
sealed class Test : Bird
{
}
// Inheriting the Sealed Class
class Example : Test
{
}
class Geeks
{
// Main Method
static void Main()
{
}
}
Output
Example 3: Consider the following example of a sealed method in a derived class.
C#
// C# program to define Sealed Class Method
using System;
class Printer
{
// Displaying dimension printing
public virtual void show()
{
Console.WriteLine("Display dimension : 6*6");
}
public virtual void print()
{
Console.WriteLine("Printer printing....\n");
}
}
// Inheriting class
class LaserJet : Printer
{
// Sealed Display Function
// for Dimension printing
sealed override public void show()
{
Console.WriteLine("Display dimension : 12*12");
}
// Function to override
// Print() function
override public void print()
{
Console.WriteLine("Laserjet printer printing....\n");
}
}
// Officejet class cannot override show
// function as it is sealed in LaserJet class.
class Officejet : LaserJet
{
// can not override show function or else
// compiler error : 'Officejet.show()' :
// cannot override inherited member
// 'LaserJet.show()' because it is sealed.
override public void print()
{
Console.WriteLine("Officejet printer printing....");
}
}
class Geeks
{
// Main Method
static void Main(string[] args)
{
Printer p = new Printer();
p.show();
p.print();
Printer ls = new LaserJet();
ls.show();
ls.print();
Printer of = new Officejet();
of.show();
of.print();
}
}
Outputdisplay dimension : 6*6
printer printing....
display dimension : 12*12
Laserjet printer printing....
display dimension : 12*12
Officejet printer printing....
Important Points
- A sealed class is used to stop a class from being inherited. We cannot derive or extend any class from it.
- The sealed method is implemented so that no other class can overthrow it and implement its method.
- The main purpose of the sealed class is to withdraw the inheritance attribute from the user so that they can’t attain a class from a sealed class.
- Sealed classes are used best when we have a class with static members.
For Example: The“Pens” and “Brushes” classes of the System.Drawing namespace. Pens class represents the pens for standard colors. This class has only static members. For example, “Pens.Red” represents a pen with red color. Similarly, the “Brushes” class represents standard brushes. “Brushes.Red” represents a brush with red color.