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.

implementation inheritance ❘ 93<br />

However, because your version of MyGroovyMethod() is not declared as new, the compiler picks up on the fact<br />

that it’s hiding a base class method without being instructed to do so <strong>and</strong> will generate a warning (this applies<br />

whether or not you declared MyGroovyMethod() as virtual). If you want, you can rename your version of<br />

the method. This is the recommended course of action because it eliminates future confusion. However, if you<br />

decide not to rename your method for whatever reason (for example, if you’ve published your software as a<br />

library for other companies, so you can’t change the names of methods), all your existing client code will still<br />

run correctly, picking up your version of MyGroovyMethod(). This is because any existing code that accesses<br />

this method must be done through a reference to MyDerivedClass (or a further derived class).<br />

Your existing code cannot access this method through a reference to HisBaseClass; it would generate<br />

a compilation error when compiled against the earlier version of HisBaseClass. The problem can only<br />

happen in client code you have yet to write. <strong>C#</strong> arranges things so that you get a warning that a potential<br />

problem might occur in future code — you will need to pay attention to this warning <strong>and</strong> take care not to<br />

attempt to call your version of MyGroovyMethod() through any reference to HisBaseClass in any future<br />

code you add. However, all your existing code will still work fine. It may be a subtle point, but it’s quite an<br />

impressive example of how <strong>C#</strong> is able to cope with different versions of classes.<br />

Calling base Versions of functions<br />

<strong>C#</strong> has a special syntax for calling base versions of a method from a derived class: base.().<br />

For example, if you want a method in a derived class to return 90 percent of the value returned by the base<br />

class method, you can use the following syntax:<br />

class CustomerAccount<br />

{<br />

public virtual decimal CalculatePrice()<br />

{<br />

// implementation<br />

return 0.0M;<br />

}<br />

}<br />

class GoldAccount: CustomerAccount<br />

{<br />

public override decimal CalculatePrice()<br />

{<br />

return base.CalculatePrice() * 0.9M;<br />

}<br />

}<br />

Note that you can use the base.() syntax to call any method in the base class — you don’t<br />

have to call it from inside an override of the same method.<br />

abstract Classes <strong>and</strong> functions<br />

<strong>C#</strong> allows both classes <strong>and</strong> functions to be declared as abstract. An abstract class cannot be instantiated,<br />

whereas an abstract function does not have an implementation, <strong>and</strong> must be overridden in any non-abstract<br />

derived class. Obviously, an abstract function is automatically virtual (although you don’t need to supply the<br />

virtual keyword; doing so results in a syntax error). If any class contains any abstract functions, that class<br />

is also abstract <strong>and</strong> must be declared as such:<br />

abstract class Building<br />

{<br />

public abstract decimal CalculateHeatingCost(); // abstract method<br />

}<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!