C# Sealed

The sealed keyword prevents other classes, methods or properties from inheriting from it.

  • Sealed Class cannot be inherited by other classes.
  • Sealed Class cannot be used with the keyword abstract. We'll introduce abstract class in a later section.
  • Sealed Class tells the compiler there is no class derived from it so it runs a little bit faster than a non-sealed class.

Sealed Class Examples:

sealed class A {}                // Defined an internal sealed class A
sealed class B : A {}            // Defined an internal sealed class B which inherited from A
public sealed class B : A {}     // Defined an public sealed class B which inherited from A
sealed public class B : A {}     // It is fine to put sealed ahead of public

Example 01-59-01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System;

public class A
{
    public virtual void test()
    {
        Console.WriteLine("Class A");
    }
}

public sealed class B : A
{
    public override void test()
    {
        Console.WriteLine("Class B");
    }    
}

//public class C : B {}

public class TestSealedClass
{
    public static void Main()
    {
        A a = new B();
        a.test();
        Console.Read();
    }
}

Output

Class B

Explanation

  • Line 3-9: Define a class with a defined virtual method test().
  • Line 11-17: Define a sealed class B derived from class A.
  • Line 19: If the comment // were removed, you would get a compile time error. Because class B is a sealed class which can not be inherited by any classes.
  • Line 21-29: Define a class for testing.
  • Line 23: Main method starts.
  • Line 25: Create an instance of class B and assigned it to a variable a with class A type. It is allowed to assign an instance of a derived class to a variable of its base class.
  • Line 26: Call the method a.test() to output "Class B" because test() is a virtual method in A and the override method in the most inherited class will be invoked.
  • The sealed keyword can be used in an overriding method or property to indicate the method or property cannot be overridden any more.
  • The sealed keyword must be used with override together in a method or property.
  • The access modifier, sealed and override can be put in any order before the returned type of a method.

The followings are legal in a method and can be compiled successfully.

public sealed override void test()
sealed protected override void test()
override protected sealed void test()

The following is illegal because sealed is put behind the return type in the method.

override protected void sealed test()

Example 01-59-02

We'll add 3 override methods for the class Rectangle in the example 01-58-01

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;

public class A
{
    public virtual void test()
    {
        Console.WriteLine("Class A");
    }
}

public class B : A
{
    public sealed override void test()
    {
        Console.WriteLine("Class B");
    }
}

public class C : B
{
    public new void test()    // Error if new is replaced with override
    {
        Console.WriteLine("Class C");
    }
}

public class TestSealedMethod
{
    public static void Main()
    {
        A a = new C();
        a.test();    // Output: Class B

        C c = new C();
        c.test();    // Output: Class C
        Console.Read();
    }
}

Output

Class B
Class C

Explanation

  • Line 3-9: Define a class with a virtual method test().
  • Line 11-17: Define a class B derived from class A.
  • Line 13-16: Define a sealed override method test() to override the same method defined in class A.
  • Line 19-25: Define a class C derived from class B.
  • Line 21-24: Define a new method test(). If the new were replaced with override, you would get a compile time error message. Because the method test() was sealed in class B and could not be overridden any more.
  • Line 27-38: Define a class for testing.
  • Line 29: Main method starts.
  • Line 31: Create an instance of class C and assigned it to a variable a with class A type. It is allowed to assign an instance of a derived class to a variable of its base class.
  • Line 32: Call the method a.test() to output "Class B" because test() is a virtual method in A and the override method in the most inherited class will be invoked. Pay attention, the method was sealed in class B so it was invoked in class B eventually.
  • Line 34-35: Instantiate class C and call its test() method to output a string. The method in class C is a new method which hides the same name's method in the base class B.