O'Reilly logo

Professional C# 2008 by Karli Watson, Morgan Skinner, Jay Glynn, Bill Evjen, Christian Nagel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 4

Inheritance

Chapter 3, “Objects and Types,” examined how to use individual classes in C#. The focus in that chapter was how to define methods, constructors, properties, and other members of a single class (or a single struct). Although you did learn that all classes are ultimately derived from the class System.Object, you did not see how to create a hierarchy of inherited classes. Inheritance is the subject of this chapter. In this chapter, you will see how C# and the .NET Framework handle inheritance. Topics covered include:

  • Types of inheritance
  • Implementing inheritance
  • Access modifiers
  • Interfaces

Types of Inheritance

Let’s start off by reviewing exactly what C# does and does not support as far as inheritance is concerned.

Implementation versus Interface Inheritance

In object-oriented programming, there are two distinct types of inheritance — implementation inheritance and interface inheritance:

  • Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and functions. With implementation inheritance, a derived type adopts the base type’s implementation of each function, unless it is indicated in the definition of the derived type that a function implementation is to be overridden. This type of inheritance is most useful when you need to add functionality to an existing type, or when a number of related types share a significant amount of common functionality. A good example of this comes in the Windows Forms classes, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required