Sealed Classes in C#
This topic contains and tries to cover following subjects:
- Explanation of Sealed Classes in C#
- Syntax of Sealed Class declaration and usage in C#
Articles provides solution to following questions and issues:
- How to declare a Sealed Class?
- How to lock a Class prevent further overrides by inheritor Classes?
- How to lock a Class from being inherited by another Classes?
- Why and where Sealed Classes are used in practice?
Explanation of Sealed Classes in C#
Sealed Classes allows programmers to seal-lock their Classes to prevent derived Classes from it to inherit or override its functions or any partial section. When a Class is marked as sealed, compiling a Class which inherits the sealed Class yields compiler error. Sealed Classes can serve to following purposes:
- Commercial reasons: The developer may want its Class to be protected from extensions by the third-party developer or related processes.
- Programmer may want that Class to be protected for unstable function overrides in derived Classes.
Syntax of Sealed Class Creation in C#
Following syntax is used to create a Sealed Class.
namespace NS_HRapplication
{
sealed class CL_baseClass
{
}
class CL_derivedClass : CL_baseClass //COMPILER ERROR: base Class is sealed...///
{
}
class CL_x
{
static void Main(string [] args)
{
}
}
}