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:
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 System;
// Sealed class
sealed class SealedClass {
// Calling Function
public int Add(int a, int b){
return a + b;
}
}
class Geeks{
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: When we try to inherit a class from a sealed class, an error will be produced stating that "It cannot be derived from a Sealed class"
C#
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:
./Solution.cs(13,7): error CS0509: `Example': cannot derive from sealed type `Test'
./Solution.cs(8,14): (Location of the symbol related to previous error)
Compilation failed: 1 error(s), 0 warnings
Example 3: Program to define Sealed Class Method
C#
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
{
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.
Explore
Introduction
Fundamentals
Control Statements
OOP Concepts
Methods
Arrays
ArrayList
String
Tuple
Indexers