PART I
C h a p t e r 1 1 :
I n h e r i t a n c e
305
PART IPART I
Using sealed to Prevent Inheritance
As powerful and useful as inheritance is, sometimes you will want to prevent it. For
example, you might have a class that encapsulates the initialization sequence of some
specialized hardware device, such as a medical monitor. In this case, you don’t want users
of your class to be able to change the way the monitor is initialized, possibly setting the
device incorrectly. Whatever the reason, in C# it is easy to prevent a class from being
inherited by using the keyword
sealed
.
To prevent a class from being inherited, precede its declaration with
sealed
. As you
might expect, it is illegal to declare a class as both
Do'stlaringiz bilan baham: |