6.7. Sealed classes (Java final classes)
A sealed class is the C# version of a Java final class. Sealing classes prevent unintended or unauthorized subclassing. It also enables certain runtime optimizations by the underlying runtime environment.
Lines 7 – 8 in the program which follows declares a sealed class called Parent. Being sealed, no other class is allowed to subclass it, hence explaining the compilation error.
1: // Negative example
2: class Child:Parent{
3: public static void Main(){
4: }
5: }
6:
7: sealed class Parent{
8: }
Compilation error:
test.cs(2,7): error CS0509: 'Child' : cannot inherit from sealed class 'Parent'
Like Java:
Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.