Therefore, when
designing classes, we recommend that you create NotInheritable classes and document the
public interface well. Consumers who need to extend the functionality can still do so, but
through containment rather than inheritance. Extension through containment, coupled with
interface definitions, is usually more powerful than class inheritance.
In rare instances, when you??™re deriving from a class with overridable methods, you may
want to force the virtual chain for a specific method to end at your override. In other words,
you don??™t want further derived classes to be able to override the overridable method. To do so,
you also mark the method with the NotOverridable modifier. As is obvious from the name, it
means that no further derived classes can override the method. They can, however, provide a
method with the same signature, as long as the method is marked with the Shadows modifier,
as discussed in the previous section. In fact, you could mark the shadowed method as overridable,
thus starting a new virtual chain in the hierarchy. This is not the same as sealing the
entire class, which doesn??™t even allow a class to derive from this one in the first place. Therefore,
if the deriving class is marked as NotInheritable, then marking override methods within
that class with NotOverridable is redundant.
Pages:
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152