Tech Point Fundamentals

Friday, October 9, 2020

Sealed Class and Sealed Method in C#

Sealed Class and Sealed Method in C# with Real Example

SealedClass-And-SealedMethods-InCSharp

  Sealing is often used to encapsulate a logic that needs to be used across the application but without any alteration to it. A sealed class is used to define the inheritance level of a class. You can restrict a class from inheritance for security reasons in C# by declaring it a sealed class. In this article, we will walk through the sealed class and sealed method with a real example.



Introduction


In C# sealed keyword applies restrictions on the class, method, property, indexer or event. If you create a sealed class, it cannot be derived. If you create a sealed method, it cannot be overridden. In C#, we can use a sealed keyword before or after the access modifier to define the class as sealed classes.

In VB.Net "NotInheritable" keyword serves the purpose of sealing. In Java "final" keyword serves the same purpose. In C# also, if you see the IL code for any sealed method, IL uses the "final" keyword internally and for a sealed method a "sealed" keyword is used.



Sealed Class


A sealed class is completely opposite to an abstract class since sealed classes prevent derivation, and an abstract class is only used for derivation. A class modified by the "sealed" keyword is known as a sealed class. Sealed classes are used to restrict the inheritance feature of object-oriented programming.

Sealed classes restrict the users from inheriting the class. The sealed keyword tells the compiler that the class cannot be extended. Normally a sealed class is always the last level class in the inheritance hierarchy, but a stand-alone class can also be marked as sealed. A sealed class can also define constructors.

The best usage of sealed classes are when you have a class with static members only because static members can be accessed directly by the class name. You can read more about the static class here.



Sealed Class Rules

    1. A sealed class cannot be inherited but can be instantiated.
    2. The sealed classes can be a derived class but cannot be a base class.
    3. A sealed class cannot define any virtual member.
    4. A sealed class cannot be abstract or static.
    5. Sealed classes cannot contain any abstract methods.
    6. A sealed class can define the constructors but a constructor cannot be marked as sealed.
    7. The sealed keyword can be used either after or before the access modifier of the class.
    8.  A field variable cannot be marked as sealed.
    9. Unlike a sealed method, a non-child class can also be defined as sealed.
    10. 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.


An interface itself cannot be declared as sealed but, it can have both virtual and sealed methods in C# 8.
You can read more about the sealed interface method in C# 8 here.



Sealed Method Rules

    1. A sealed method can be only be defined in a derived or child class.
    2. The sealed keyword is always used with the override keyword.
    3. Only virtual methods of the base class can be overridden as sealed.
    4. An abstract method can also be overridden as sealed in the derived class since an abstract method is virtual implicitly.
    5. A field variable cannot be marked as sealed, because it cannot be overridden.
    6. You can specify the sealed modifier before the access modifier, before or after the override keyword.
    7. In C# 8, an interface can also define a sealed method without inheriting any base interface.
    8. A virtual interface method cannot be overridden as sealed in the derived interface.
    9. 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

The compiler generates different keyword for class and method for making it as sealed. If you see the IL code of the above program in IL disassembler (ILDASM Tool):

SealedClassAndMethod-ILCode
Click on image to see full view

For the sealed class "DerivedClass" the IL code generated by the compiler is:
".class public auto ansi sealed beforefieldinit SealedClassAndMethod.DerivedClass extends [mscorlib]System.Object"  

For the sealed method "Print()" the IL code generated by the compiler is:
".method public hidebysig virtual final instance void Print() cil managed"  

Notice there is a "final" keyword for the sealed method not "sealed" unlike sealed class.



Sealed Method Vs Private Method in C#

    1. A private method is not inherited whereas a sealed method is inherited but cannot be overridden.
    2. A private method cannot be accessed from derived-classes while a sealed method can be accessed from derived-classes.
    3. Only a virtual member can be sealed in the derived class, but there is no such condition for making a method as private.
    4. A sealed method can only be declared in a child class while a private method can be defined in any class.
    5. 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#

    1. An abstract class can only be used as a base class, while a sealed class cannot be used as a base class.
    2. An abstract class contains abstract methods but a sealed class cannot contain any abstract method.
    3. An abstract class contains virtual methods but a sealed class cannot contain any virtual method.
    4. An abstract class cannot be instantiated, but a sealed class can be instantiated.
    5. 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:

    1. A static class cannot be instantiated while a sealed class can be instantiated.
    2. A static class cannot define any indexer while a sealed class can define the indexers.
    3. Static class cannot have any destructor while a sealed class can have a destructor.
    4. Static class cannot have any instance constructor while a sealed class can have both static and instance constructors.
    5. A static class cannot inherit from any class or interface, but a sealed class can inherit from a class or interface.
    6. A static class can only contain static members, but a sealed class can have both static and non-static members.
    7. 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.
    8. 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.

You can read more about the static class here.



Real Examples of Sealed Class

    1. System.String class is a sealed class.
    2. All the structs are sealed implicitly, so, they cannot be inherited by any other struct and no class can inherit from any struct.
    3. All the enums are also sealed implicitly.
    4. All the value types are sealed.
    5. All the static classes are sealed implicitly.



Why Sealed Class

1. Invariability: By making any class as sealed, we prevent it from being inherited by any other user. It maintains the quality of uniformity and lack of variation.

2. Security: A sealed class is mostly used for security reasons. By preventing unintended derivation from a class we can restrict the derived class from corrupting the implementation provided in the sealed class.

3. Speed: Since a sealed class can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster because for a sealed class runtime emits "call" instead of "callvirt".



Live Demo


 



1 comment:

Please do not enter any HTML. JavaScript or spam link in the comment box.