C# Simplified, part 4: Structures, Inheritance and Interfaces - Sealed classes
(Page 4 of 7 )
Classes declared with the sealed keyword cannot be extended. That means, when you declare a base class with this keyword, you cannot use it in a derived class. In other words the class cannot be extended. This keyword is similar to that of final keyword in Java.
Listing 4.4 is a modified version of listing 4.3. It declares the base class Computer as sealed.
Listing 4.4
// HardDriveSealed.cs
using System;
// error location
sealed class Computer
{
// Erase the Protected modifier and observe the result.
public void Calls()
{
int num1 = 10;
int num2 = 20;
int num3 = num1+num2;
Console.WriteLine(num3);
}
}
class HardDriveSealed: Computer
{
public static void Main()
{
HardDrive hd = new HardDrive();
hd.Calls();
}
}
When you execute the above code, you will get a compilation error indicating that the derived class cannot inherit from the sealed base class Computer.
Next: Abstract classes >>
More C# Articles
More By Anand Narayanaswamy