Abstract vs Sealed
Abstract or Sealed: When to Use Each in .NET?
In .NET, the decisions to make a class abstract or sealed depend on the design and intended usage of the class. Let’s discuss when to use each.
Using Abstract Classes:
Use abstract classes when you have a class that should not be instantiated on its own because it contains one or more methods without implementation (abstract methods).
Abstract classes are useful when you want to define a common base class that provides some common functionality to all its derived classes.
For example, for setting a base class or when you want your derived class to override some methods, then use abstract.
Using Sealed Classes:
Use sealed classes when you want to prevent further inheritance of a class.
If you’ve designed a class to be used as-is without any further specialization.
You want to achieve performance benefits, as the compiler can make certain optimizations knowing that there won’t be any derived classes.
Sealing a class can enhance security by preventing unintended modifications to the class’s behavior through inheritance.
For example, make your validators and services sealed. We know no one will inherit from them except for your conditions.