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:
|