Class in C# can be declared using sealed keyword, using sealed keyword enables you to prevent the inheritance of a class or certain class members that were previously marked virtual.
The sealed modifier is used to prevent derivation from a class. An error occurs if a sealed class is specified as the base class of another class.
Sealed class and class members in C#
Classes can be declared as sealed by putting the keyword sealed
before the class definition. For example:
public sealed class SealedClassDemo
{
// Class members here.
}
C# sealed class cannot be derived by any class, here are the few points to remember for sealed classes :
- A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class.
- Sealed classes prevent derivation.
- Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster.
- Sealed class is the last class in the hierarchy.
Let's see an example when we try to inherit sealed class in C#, suppose here the Code
using System;
namespace CSharpSealedClass
{
public sealed class Animal
{
public void eat() { Console.WriteLine("eating..."); }
}
public class Dog : Animal
{
public void bark() { Console.WriteLine("barking..."); }
}
public class Program
{
public static void Main(string[] args)
{
Dog d = new Dog();
d.eat();
d.bark();
}
}
}
You will get the error as shown in below in the image
Now, let's take an working example for sealed class in C#
using System;
namespace CSharpSealedClass
{
public sealed class SealedClass
{
public int x;
public int y;
}
class CSharpSealedClass
{
static void Main()
{
SealedClass sc = new SealedClass();
sc.x = 110;
sc.y = 150;
Console.WriteLine("x = {0}, y = {1}", sc.x, sc.y);
Console.ReadLine();
}
}
}
When executing above code, you will get output as below
Sealed methods
A method, indexer, property, or event, on a derived class that is overriding a virtual member of the base class can declare that member as sealed. This negates the virtual aspect of the member for any further derived class. This is accomplished by putting the sealed
keyword before the override keyword in the class member declaration
For example:
public class D : C
{
public sealed override void DoWork() { }
}
Here is the more detailed example, in the following example, Z
inherits from Y
but Z
cannot override the virtual function F
that is declared in X
and sealed in Y
.
using System;
namespace CSharpSealedClass
{
class X
{
public virtual void F()
{
Console.WriteLine("X.F");
}
public virtual void F2()
{
Console.WriteLine("X.F2");
}
}
class Y : X
{
sealed public override void F()
{
Console.WriteLine("Y.F");
}
public override void F2()
{
Console.WriteLine("Y.F2");
}
}
class Z : Y
{
// Attempting to override F causes compiler error CS0239.
// protected override void F() { Console.WriteLine("Z.F"); }
// Overriding F2 is allowed.
public override void F2()
{
Console.WriteLine("Z.F2");
}
}
class CSharpSealedClass
{
static void Main()
{
Z sm = new Z();
sm.F();
sm.F2();
Console.ReadLine();
}
}
}
Output:
Y.F
Z.F2
When you define new methods or properties in a class, you can prevent deriving classes from overriding them by not declaring them as virtual.
It is an error to use the abstract modifier with a sealed class, because an abstract class must be inherited by a class that provides an implementation of the abstract methods or properties.
When applied to a method or property, the sealed
modifier must always be used with override.
Because structs are implicitly sealed, they cannot be inherited.
That's it, hope this article cleared your concepts related to Sealed class & methods in C#.