15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

94 ❘ ChaPTer 4 inheritAnce<br />

sealed Classes <strong>and</strong> methods<br />

<strong>C#</strong> allows classes <strong>and</strong> methods to be declared as sealed. In the case of a class, this means that you can’t<br />

inherit from that class. In the case of a method, this means that you can’t override that method.<br />

sealed class FinalClass<br />

{<br />

// etc<br />

}<br />

class DerivedClass: FinalClass<br />

{<br />

// etc<br />

}<br />

// wrong. Will give compilation error<br />

The most likely situation in which you’ll mark a class or method as sealed is if the class or method is<br />

internal to the operation of the library, class, or other classes that you are writing, so that you ensure that<br />

any attempt to override some of its functionality will lead to instability in the code. You might also mark a<br />

class or method as sealed for commercial reasons, in order to prevent a third party from extending your<br />

classes in a manner that is contrary to the licensing agreements. In general, however, you should be careful<br />

about marking a class or member as sealed because by doing so you are severely restricting how it can be<br />

used. Even if you don’t think it would be useful to inherit from a class or override a particular member of it,<br />

it’s still possible that at some point in the future someone will encounter a situation you hadn’t anticipated<br />

in which it is useful to do so. The .<strong>NET</strong> base class library frequently uses sealed classes to make these<br />

classes inaccessible to third-party developers who might want to derive their own classes from them. For<br />

example, string is a sealed class.<br />

Declaring a method as sealed serves a similar purpose as for a class:<br />

class MyClass: MyClassBase<br />

{<br />

public sealed override void FinalMethod()<br />

{<br />

// etc.<br />

}<br />

}<br />

class DerivedClass: MyClass<br />

{<br />

public override void FinalMethod() // wrong. Will give compilation error<br />

{<br />

}<br />

}<br />

In order to use the sealed keyword on a method or property, it must have first been overridden from a base<br />

class. If you do not want a method or property in a base class overridden, then don’t mark it as virtual.<br />

Constructors of derived Classes<br />

Chapter 3 discusses how constructors can be applied to individual classes. An interesting question arises<br />

as to what happens when you start defining your own constructors for classes that are part of a hierarchy,<br />

inherited from other classes that may also have custom constructors.<br />

Assume that you have not defined any explicit constructors for any of your classes. This means that the<br />

compiler supplies default zeroing-out constructors for all your classes. There is actually quite a lot going<br />

on under the hood when that happens, but the compiler is able to arrange it so that things work out nicely<br />

throughout the class hierarchy <strong>and</strong> every field in every class gets initialized to whatever its default value is.<br />

When you add a constructor of your own, however, you are effectively taking control of construction. This<br />

has implications right down through the hierarchy of derived classes, <strong>and</strong> you have to make sure that you<br />

don’t inadvertently do anything to prevent construction through the hierarchy from taking place smoothly.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!