Introduction to Sealed in C#
In C# sealed keyword is used for preventing the other classes to be inheriting from it. We can also use the sealed keyword to the modifiers on a property or a method that overrides the property of the parent class or base class. So basically it is used when we need to do restrictions to inherit the class. Compiler read the keyword sealed and understand that it cannot be extended. The sealed class can not be a base class as it can not be inherited by any other class. If a class tries to drive a sealed class, the C# compiler would generate an error message.
Syntax:
We can use the keyword sealed as below
classX {}
sealedclassY : X {}
We can use the keyword sealed for the method also. If any method is sealed then we cannot override that method but a method can only be sealed in the inherited classes. It is not always necessary that sealed class have sealed methods.
But if we try to inherit a class from sealed class then we will get the error which will be “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 in code we are trying to extend the sealed class.
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 it or implement it. So this is mostly used for security purposes.
- It is mostly 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 ball.white is represent the ball of white color. Similarly bat class represents the standard bats only. So when we are designing any class and we want to restrict it so that developers can not extend it further, then sealed keyword can be used.
- Sealed method is always an override method of the child class.
- We can not again override the sealed method.
- 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 the 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, the sealed class Example is created and the object of a sealed class is called 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, then 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 is showing that sealed class cannot be derived by any other 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, there is a sealed method that cannot be overridden. Therefore it will give an error.
Output:
Conclusion
In the end, we can say that 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. Sealed class cannot be derived by any other class and sealed method cannot be overridden. Therefore it is a restriction to avoid the inheritance.
Recommended Articles
This is a guide to Sealed in C#. Here we discuss a brief overview of Sealed in C# and its examples along with its code implementation. You can also go through our other suggested articles to learn more –
6 Online Courses | 18 Hands-on Project | 90+ Hours | Verifiable Certificate of Completion
4.6
View Course
Related Courses