O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required