15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

4inheritance<br />

WhaT ’ s in This ChaPTer<br />

➤<br />

➤<br />

➤<br />

➤<br />

Types of inheritance<br />

Implementing inheritance<br />

Access modifi ers<br />

Interfaces<br />

Chapter 3, “ Objects <strong>and</strong> Types, ” examined how to use individual classes in <strong>C#</strong>. The focus in that<br />

chapter was how to defi ne methods, properties, constructors, <strong>and</strong> other members of a single<br />

class (or a single struct). Although you learned that all classes are ultimately derived from the class<br />

System.Object , you did not learn how to create a hierarchy of inherited classes. Inheritance is the<br />

subject of this chapter. In this chapter, you see how <strong>C#</strong> <strong>and</strong> the .<strong>NET</strong> Framework h<strong>and</strong>le inheritance.<br />

TyPes of inheriTanCe<br />

Let ’ s start by reviewing exactly what <strong>C#</strong> does <strong>and</strong> does not support as far as inheritance is concerned.<br />

implementation Versus interface inheritance<br />

In object - oriented programming, there are two distinct types of inheritance — implementation<br />

inheritance <strong>and</strong> interface inheritance:<br />

➤<br />

➤<br />

Implementation inheritance means that a type derives from a base type, taking all the base<br />

type ’ s member fi elds <strong>and</strong> functions. With implementation inheritance, a derived type adopts<br />

the base type ’ s implementation of each function, unless it is indicated in the defi nition of the<br />

derived type that a function implementation is to be overridden. This type of inheritance is<br />

most useful when you need to add functionality to an existing type, or when a number of<br />

related types share a signifi cant amount of common functionality.<br />

Interface inheritance means that a type inherits only the signatures of the functions <strong>and</strong> does<br />

not inherit any implementations. This type of inheritance is most useful when you want to<br />

specify that a type makes certain features available.<br />

<strong>C#</strong> supports both implementation <strong>and</strong> interface inheritance. Both are baked into the framework<br />

<strong>and</strong> the language from the ground up, thereby allowing you to decide which to use based on the<br />

architecture of the application.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!