11.1 Introduction

This chapter continues our discussion of object-oriented programming (OOP) by introducing one of its primary features—inheritance, 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. Inheritance lets you save time during app development by reusing proven, high-performance and debugged high-quality software. This also increases the likelihood that a system will be implemented effectively.

The existing class from which a new class inherits members 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, properties and methods. Therefore, it’s 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 ones that are specific to itself.

The direct base class is the base class from which a 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—a C# keyword that’s an alias for System.Object in the Framework Class Library. Every class directly or indirectly extends (or “inherits from”) object. Section 11.7 lists class object’s methods, which every other class inherits. In single inheritance, a class is derived from one direct base class. C# supports only single inheritance. In Chapter 12, OOP: Polymorphism and Interfaces, we explain how you can use interfaces to realize many of the benefits of multiple inheritance (i.e., inheriting from multiple direct base classes) while avoiding the associated problems that occur in some programming languages.

Experience in building software systems indicates that significant amounts of code deal with closely related special cases. When you’re preoccupied with special cases, the details can obscure the big picture. With object-oriented programming, you 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 also can 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 10). 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset