Introduction

This chapter continues our discussion of object-oriented programming (OOP) by introducing one of its primary featuresinheritance, which is a form of software reuse in which a new class is created by absorbing an existing class's members and enhancing them with new or modified capabilities. With inheritance, programmers save time during application development by reusing proven and debugged high-quality software. This also increases the likelihood that a system will be implemented effectively.

When creating a class, rather than declaring completely new members, you can designate that the new class should inherit the members of an existing class. The existing class is called the base class, and the new class is the derived class. Each derived class can become the base class for future derived classes.

A derived class normally adds its own fields and methods. Therefore, a derived class is more specific than its base class and represents a more specialized group of objects. Typically, the derived class exhibits the behaviors of its base class and additional behaviors that are specific to the derived class.

The direct base class is the base class from which the derived class explicitly inherits. An indirect base class is any class above the direct base class in the class hierarchy, which defines the inheritance relationships among classes. The class hierarchy begins with class object (which is the C# alias for System.Object in the Framework Class Library), which every class directly or indirectly extends (or "inherits from"). Section 10.7 lists the methods of class object, which every other class inherits. In the case of single inheritance, a class is derived from one direct base class. C#, unlike C++, does not support multiple inheritance (which occurs when a class is derived from more than one direct base class). In Chapter 11, Polymorphism, Interfaces & Operator Overloading, we explain how you can use interfaces to realize many of the benefits of multiple inheritance while avoiding the associated problems.

Experience in building software systems indicates that significant amounts of code deal with closely related special cases. When programmers are preoccupied with special cases, the details can obscure the big picture. With object-oriented programming, programmers can, when appropriate, focus on the commonalities among objects in the system rather than the special cases.

We distinguish between the is-a relationship and the has-a relationship. Is-a represents inheritance. In an is-a relationship, an object of a derived class can also be treated as an object of its base class. For example, a car is a vehicle, and a truck is a vehicle. By contrast, has-a represents composition (see Chapter 9). In a has-a relationship, an object contains as members references to other objects. For example, a car has a steering wheel, and a car object has a reference to a steering wheel object.

New classes can inherit from classes in class libraries. Organizations develop their own class libraries and can take advantage of others available worldwide. Some day, most new software likely will be constructed from standardized reusable components, just as automobiles and most computer hardware are constructed today. This will facilitate the development of more powerful, abundant and economical software.

Категории