Chapter 5: Inheritance


Life would be so much easier if a long-lost relative left you his multimillion-dollar estate. You could be ahead of the game, picking up where your relative left off by benefiting from his hard work. Get real. You probably have a better chance of becoming President of the United States. However, you can pick up where other programmers left off by inheriting their work into your program. It s not the same as inheriting a multimillion-dollar estate, but inheriting part of a program saves you countless hours of programming ”and avoids a lot of head banging. Inheritance is a cornerstone of object-oriented programming and is the topic of this chapter.

Inheritance: The Only Way to Program

Inheritance in object-oriented programming is very similar to the way we inherit characteristics from our parents. Characteristics in object-oriented programming terms are attributes and behaviors of a class ”that is, the data and methods of a class.

Biological inheritance creates a hierarchical classification that enables you to trace your heritage to generations of relatives who have come before you. The same is true in object-oriented programming. A hierarchical relationship develops as classes inherit from other classes within a program. You can trace this relationship to determine the origins of a class.

Inheritance is a cornerstone of object-oriented programming because it enables objects to inherit attributes and behaviors from other objects, thereby reducing the amount of new code that must be designed, written, and tested each time a new program is developed.

In Chapter 4, you saw how a graduate student object inherits attributes and behaviors of a student object, which are common to all kinds of students. The programmer who created the graduate student object had to focus on defining attributes and behaviors that are common to a graduate student and not those of a student.

Inheritance provides a way to distribute control for development and maintenance of objects. For example, a programmer might be responsible for creating and maintaining the student object. Another programmer might develop and maintain the graduate student object. Whenever a change occurs that affects all students, those changes are made to the student object and are then inherited by the graduate student object. Only the programmer responsible for the student object needs to address those changes because the graduate student object inherits any changes made to the student object.

Inheritance also provides a way to limit access to attributes and behaviors. Previously in Chapter 4, you learned how the public, private, and protected access specifiers are used to determine parts of the program that can access attributes and behaviors.

Members of a class defined using the public access specifier are available to other members of the class, other classes, and to all parts of the program.

Those members defined using the private access specifier are only accessible by members of the class. They are unavailable to other classes and other parts of the program. The protected access specifier identifies attributes and behaviors that are available to members of the class and available to other classes inherited from it.

The purpose of limiting access to attributes and behaviors is to ensure the integrity of the object by controlling how other classes and parts of the program interact with it. A good example of this was shown in Chapter 4. A member method of the student class must be used to access the names of students. The student first name and last name attributes cannot be accessed directly either by another class or from within the program. The member method contains routines to ensure the integrity of student names .




OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

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