InterviewSolution
Saved Bookmarks
| 1. |
Sealed Classes in C# |
|
Answer» SEALED classes are used to restrict inheritance. In other words, sealed classes are those classes that cannot be extended and no class can be DERIVED from them. The sealed keyword is used to CREATE a sealed class in C#. A program that demonstrates sealed classes in C# is given as follows: using System; sealed class Product { public int returnProduct(int num1, int num2) { return num1 * num2; } } class Demo { static void Main(string[] args) { Product obj = new Product(); Console.WriteLine("The product of 3 and 5 is: " + obj.returnProduct(3,5)); } }The output of the above program is as follows: The product of 3 and 5 is: 15In the above program, the class Product is sealed class. This means that it cannot be INHERITED and an error will be generated if a class is derived from class Product. |
|