InterviewSolution
Saved Bookmarks
| 1. |
What are sealed classes in C#? |
|
Answer» If we want to prevent any class from being inherited, it can be done with the help of a sealed keyword. So using a sealed keyword, a class denies the ability to create any child entity of this class, thereby calling itself a sealed class. If by mistake, a child of a sealed class is tried to be made, a compile-time error will occur. using System;// Sealed class sealed class SealedClass { public int Add(int x, int y) { return x + y; } } class Class1: SealedClass { static void Main(string[] args) { SealedClass sealedCls = new SealedClass(); int total = sealedCls.Add(4, 5); Console.WriteLine("Total = " + total.ToString()); } } Compilation error: prog.cs(12,7): error CS0509: `Class1': cannot derive from sealed type `SealedClass'prog.cs(4,14): (Location of the symbol related to previous error) Compilation failed: 1 error(s), 0 warnings |
|