Blocking Inheritance

Allowing inheritance yields a great amount of flexibility with regard to API design. At the same time, it comes as a big responsibility for the designers of the base classes. If the behavior of a base class method changes, it might break subtypes. This is especially true if virtual methods are used, as you will see later.

To avoid such responsibilities, you can mark a class as noninheritable by means of the sealed keyword. Doing so prevents anyone from inheriting from the class:

sealed class Student : Person{    ...}class GraduateStudent : Student  // fails to compile{}

Figure 14.8 shows the error emitted by the compiler in this case. Later you see how you can apply the sealed keyword to a member. ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.