The opposite side of the design coin from abstract is sealed. In contrast to an abstract class, which is intended to be derived from and to provide a template for its subclasses to follow, a sealed class does not allow classes to derive from it at all. The sealed
keyword placed before the class declaration prevents any classes from deriving from it. Classes are most often marked sealed
to prevent accidental (or intentional) inheritance.
If you change the declaration of Control
in Example 11-3 from abstract
to sealed
(eliminating the abstract
keyword from the DrawControl( )
declaration as well), the program fails to compile. If you try to build this project, the compiler returns the following error message:
'ListBox' cannot inherit from sealed type 'Class'
among many other complaints (such as that you cannot create a new protected member in a sealed class).
Microsoft recommends using sealed
when you know that you won’t need to create derived classes, and also when your class consists of nothing but static methods and properties.