Sealed Class and Sealed Method in C# with Real Example
Introduction
Sealed Class
Sealed Class Rules
- A sealed class cannot be inherited but can be instantiated.
- The sealed classes can be a derived class but cannot be a base class.
- A sealed class cannot define any virtual member.
- A sealed class cannot be abstract or static.
- Sealed classes cannot contain any abstract methods.
- A sealed class can define the constructors but a constructor cannot be marked as sealed.
- The sealed keyword can be used either after or before the access modifier of the class.
- A field variable cannot be marked as sealed.
- Unlike a sealed method, a non-child class can also be defined as sealed.
- An interface cannot be defined as sealed.
Example
Sealed Method
A sealed method is used to define the overriding level of a virtual method in the inheritance. A method modified by the "sealed" keyword is known as a sealed method. A sealed keyword is used for a method to prevent it from being overridden in the derived class, i.e. to prevent the runtime polymorphic feature of OOPs.
The sealed methods in C# cannot be overridden further, but it must be used with an override keyword in the method. If you want to declare a method as sealed, then it has to be declared as virtual in its base class because a non-virtual method cannot be overridden.
A sealed method enables you to allow classes to derive from your class but prevent your specific methods from overriding by the derived class. A property can also be sealed, but you can only seal the property not the underlying setter and getter since C# offers no override syntax for setters or getters.
Sealed Method Rules
- A sealed method can be only be defined in a derived or child class.
- The sealed keyword is always used with the override keyword.
- Only virtual methods of the base class can be overridden as sealed.
- An abstract method can also be overridden as sealed in the derived class since an abstract method is virtual implicitly.
- A field variable cannot be marked as sealed, because it cannot be overridden.
- You can specify the sealed modifier before the access modifier, before or after the override keyword.
- In C# 8, an interface can also define a sealed method without inheriting any base interface.
- A virtual interface method cannot be overridden as sealed in the derived interface.
- You can only prevent a sealed method from being overridden in the derived class, but it cannot prevent a sealed method from redefining it as virtual again.
IL Codes for Sealed Class and Sealed Methods
![]() |
Click on image to see full view |
Sealed Method Vs Private Method in C#
- A private method is not inherited whereas a sealed method is inherited but cannot be overridden.
- A private method cannot be accessed from derived-classes while a sealed method can be accessed from derived-classes.
- Only a virtual member can be sealed in the derived class, but there is no such condition for making a method as private.
- A sealed method can only be declared in a child class while a private method can be defined in any class.
- The same private method can be defined in the derived-class and it does not lead to an error or warning.
Sealed Class Vs Abstract Class in C#
- An abstract class can only be used as a base class, while a sealed class cannot be used as a base class.
- An abstract class contains abstract methods but a sealed class cannot contain any abstract method.
- An abstract class contains virtual methods but a sealed class cannot contain any virtual method.
- An abstract class cannot be instantiated, but a sealed class can be instantiated.
- An abstract class cannot be the bottom-most level in the inheritance hierarchy but a sealed class may be the bottom-most.
Sealed Class Vs Static Class in C#
Although both sealed class and a static class cannot be inherited by any class or struct, following are the difference between them:
- A static class cannot be instantiated while a sealed class can be instantiated.
- A static class cannot define any indexer while a sealed class can define the indexers.
- Static class cannot have any destructor while a sealed class can have a destructor.
- Static class cannot have any instance constructor while a sealed class can have both static and instance constructors.
- A static class cannot inherit from any class or interface, but a sealed class can inherit from a class or interface.
- A static class can only contain static members, but a sealed class can have both static and non-static members.
- Members of a static class are accessed by the class name itself while sealed class members can only be accessed by the object of the class.
- Only a single global instance is created by the runtime which remains in the memory for the lifetime of the app domain but, it is not true for the sealed class.
Real Examples of Sealed Class
- System.String class is a sealed class.
- All the structs are sealed implicitly, so, they cannot be inherited by any other struct and no class can inherit from any struct.
- All the enums are also sealed implicitly.
- All the value types are sealed.
- All the static classes are sealed implicitly.
Sup...
ReplyDelete