Sealed Class
Sealed class is a special kind of class in C# which is used to prevent the inheritance to next level. Any class with sealed keyword cannot be derived to any subclass. We will get a compile error if we try to define base class as sealed class.
sealed class A { }
class B: A { } //CompileError
In the above example if we see, Class B tries to inherit from class A which is sealed. We will get a compiler error.
Now if we move the sealed keyword from Class A to Class B and try to inherit from class B we will get a compile error ('C': cannot derive from sealed type 'B').
class A { }
sealed class B: A { }
class C : B { } //Compile Error
Below code should be fine, as Sealed class should be the last in the inheritance hierarchy.
class A { }
class B : A { }
sealed class C : B { }
In the below example if we see, Class A has 2 virtual methods Display1 and Display2. Class B inherits from Class A, in Class B we have sealed Display1 method as we don't want this method to be overridden in the deriving class. Now Class C inherits from Class B, if we try to override Display1 method of Class B, we get a compile error saying ( 'C.Display1()': cannot override inherited member 'B.Display1()' because it is sealed) but we can still override Display2 method as we didn't Seal it.
class B : A {
sealed protected override void Display1()
{
Console.WriteLine("B.Display1");
}
protected override void Display2()
{
Console.WriteLine("B.Display2");
}
}
class C : B {
//protected override void Display1() //Compile Error
//{
// Console.WriteLine("A.Display1");
//}
protected override void Display2()
{
Console.WriteLine("A.Display2");
}
}
sealed class A { }
class B: A { } //CompileError
In the above example if we see, Class B tries to inherit from class A which is sealed. We will get a compiler error.
Now if we move the sealed keyword from Class A to Class B and try to inherit from class B we will get a compile error ('C': cannot derive from sealed type 'B').
class A { }
sealed class B: A { }
class C : B { } //Compile Error
Below code should be fine, as Sealed class should be the last in the inheritance hierarchy.
class A { }
class B : A { }
sealed class C : B { }
- Sealed and Abstract modifiers cannot be used together as Abstract Class has to provide the functionality and Sealed will prevent that.
- Sealed class cannot be a base class, it can only be the last derived class in the hierarchy.
- Structs are implicitly sealed.
- Sealed class provides performance benefits also, it depends on execution environments.
Sealed Methods
We can use sealed modifier on methods or properties also which override a virtual method or property of a base class. Sealed keyword helps us to allow classes to derive from our class and prevent them from overriding the virtual methods/properties.In the below example if we see, Class A has 2 virtual methods Display1 and Display2. Class B inherits from Class A, in Class B we have sealed Display1 method as we don't want this method to be overridden in the deriving class. Now Class C inherits from Class B, if we try to override Display1 method of Class B, we get a compile error saying ( 'C.Display1()': cannot override inherited member 'B.Display1()' because it is sealed) but we can still override Display2 method as we didn't Seal it.
class A {
protected virtual void Display1()
{
Console.WriteLine("A.Display1");
Console.WriteLine("A.Display1");
}
protected virtual void Display2()
{
Console.WriteLine("A.Display2");
}
}
class B : A {
sealed protected override void Display1()
{
Console.WriteLine("B.Display1");
}
protected override void Display2()
{
Console.WriteLine("B.Display2");
}
}
class C : B {
//protected override void Display1() //Compile Error
//{
// Console.WriteLine("A.Display1");
//}
protected override void Display2()
{
Console.WriteLine("A.Display2");
}
}
- Sealed keyword is always used with Override keyword.
- Sealed method is not used with Sealed Class.
- Sealed method is used to define the overriding level of a virtual method.
Hope this article helps you in understanding the concept of Sealed identifier.
For any queries and suggestions, comment below :)
For any queries and suggestions, comment below :)
Data embody the frequencies of each of the 38 sectors over 904 spins and frequencies of the 22,527 "straight-up" bets positioned. Summary data can also be|can 퍼스트카지노 be} out there on how bettors react to streaks in even-money outcomes—allowing one to check for the gambler's fallacy. The case is used in an introductory course on data evaluation.
ReplyDelete