Updated June 17, 2023
Introduction to Sealed in C#
In C# sealed keyword is used to prevent the other classes from being inherited from it. We can also use the sealed keyword to the modifiers on a property or a method that overrides the parent or base class property. So basically, it is used when we need to do restrictions to inherit the class. The compiler reads the keyword sealed and understands it cannot be extended. The sealed class can not be a base class, as any other class can not inherit it. If a class tries to drive a sealed class, the C# compiler will generate an error message.
Syntax:
We can use the keyword sealed below
classX {}
sealedclassY : X {}
We can use the keyword sealed for the method also. We cannot override a sealed method. However, only in the inherited classes can we seal a method. It is not always necessary that sealed class have sealed methods.
But if we try to inherit a class from a sealed class, we will get the error “we cannot derive from the sealed class.” Below is the sample to show a sealed class.
using System;
class Test1 {
}
// Creating a sealed class
sealed class Test : Test {
}
// Inheriting the Sealed Class
class Example : Test1 {
}
// Driver Class
class Test1 {
// Main Method
static void Main()
{
}
}
This program will throw an error because we are trying to extend the sealed class in the code.
Uses of Sealed in C#
Below are some of the uses of Sealed in C#:
- We cannot extend or drive any other class from the sealed class.
- We can use it for methods also so that no other class can modify or implement it. People primarily use this for security reasons.
- It is mainly used when we have static members. For example, bat and ball classes of the system.cricket namespace. The ball represents the ball which is of standard colors. This ball class has static members only. Like a ball.white represents the ball of white color. Similarly, the bat class represents the standard bats only. When designing a class and aiming to restrict its extension by developers, the “sealed” keyword can be used.
- The sealed method is always an override method of the child class.
- We can not again override the sealed method.
- The sealed method is only available with the method overriding.
- The sealed keyword is not available with the method of hiding.
- Sealed is used together with the override method.
- We can not make a normal method as sealed.
Examples to Implement Sealed in C#
Below are a few examples that show how we can implement sealed in C#.
Example #1
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
sealed class Example
{
public int Sum(int i, int j)
{
return i + j;
}
}
class Program
{
// Main Method
static void Main(string[] args)
{
Example ex = new Example(); // object of sealed class
int cal = ex.Sum(21, 4); // calculate the sum
Console.WriteLine("Total = " + cal.ToString());
Console.ReadLine();
}
}
In the above example, we create a sealed class called “Example.” We then call an object of the sealed class and display the sum of integers.
Output:
Example #2
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Example1
{
}
sealed class Example2 : Example1 // sealed class
{
}
class Example3 : Example2 // Inheriting the Sealed Class
{
}
class Program
{
// Main Method
static void Main()
{
}
}
In the above example, there are multiple classes, but when a class tries to inherit a sealed class, it will give an error that “Example3 cannot derive from sealed type Example2”.
Output:
Example #3
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
sealed public class Bird // sealed class
{
public void eat()
{
Console.WriteLine("Birds are eating...");
}
}
public class Peacock : Bird // class inherits sealed type
{
public void fly()
{
Console.WriteLine("Peacock is flying...");
}
}
public class TestSealed
{
public static void Main() //main method
{
Peacock p = new Peacock();
p.eat();
p.fly();
}
}
The above example shows that any other class cannot derive a sealed class. This program will throw the error because we can not derive the sealed parent class.
Output:
Example #4
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
public class Bird
{
public virtual void fly()
{
Console.WriteLine("Birds are flying...");
}
public virtual void eat()
{
Console.WriteLine("Birds are eating...");
}
}
public class Peacock : Bird // inheritance
{
public override void fly()
{
Console.WriteLine("Peacock is flying…….");
}
public sealed override void eat() // sealed method
{
Console.WriteLine("Peacock is eating..........");
}
}
public class Peahen : Peacock // inheritance
{
public override void fly()
{
Console.WriteLine("Flying...");
}
public override void eat()
{
Console.WriteLine("eating....");
}
}
public class TestSealed
{
public static void Main() // main method
{
Peahen p = new Peahen();
p.eat();
p.fly();
}
}
In the above example, a sealed method cannot be overridden. Therefore it will give an error.
Output:
Conclusion
In the end, we can say that a sealed class or sealed method is very useful when we don’t want to expose the functionality to the derived class or when we want to restrict the code so that developers can not extend it. Any other class cannot derive a sealed class, and a sealed method cannot be overridden. Therefore it is a restriction to avoid inheritance.
Recommended Articles
This is a guide to Sealed in C#. Here we discuss a brief overview of Sealed in C#, its examples, and its code implementation. You can also go through our other suggested articles to learn more –