Section 10.1. Introduction


10.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 embellishing them with new or modified capabilities. With inheritance, you can save time during program development by reusing proven and debugged high-quality software. This 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 will inherit the members of an existing class. The existing class is called the base class, and the new class is the derived class. (The Java programming language refers to the base class as the superclass and the derived class as the subclass.) A derived class can become the base class for future derived classes.

A derived class normally adds its own instance variables, Shared variables, properties 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. [Note: A class's instance variables and Shared variables are referred to collectively as the class's fields.]

The direct base class is the class from which the derived class explicitly inherits. An indirect base class is inherited from two or more levels up in the class hierarchy. In Visual Basic, the class hierarchy begins with class Object (in namespace System), which every class in Visual Basic directly or indirectly extends (or "inherits from"). Section 10.7 lists the seven methods of class Object, which every other class inherits. In the case of single inheritance, a class is derived from one direct base class. Visual Basic, unlike C++, does not support multiple inheritance (which occurs when a class is derived from more than one direct base class). In Chapter 11, Object-Oriented Programming: Polymorphism, we explain how you can use Interfaces to realize many of the benefits of multiple inheritance while avoiding the associated problems.

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. By contrast, the has-a relationship represents composition (see Chapter 9). In a has-a relationship, an object contains one or more object references as members. 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 will probably be constructed from standardized reusable components, just as automobiles and most computer hardware items are constructed today. This will facilitate the development of more powerful, abundant and economical software.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net