Inheritance


Inheritance is an important feature of object-oriented programming because it is a way for one object to receive attributes and behaviors of another object in what programmers call an is a relationship. As an example, let s use the Person and Student objects to discuss how inheritance works.

Previously in this chapter, you learned that Person has many attributes and behaviors. In this example, we ll limit Person s attributes and behaviors to those shown in Table 1-1.

Table 1-1: Attributes and Behaviors of Person

Attributes

Behaviors

First Name

Sitting

Last Name

Standing

Address

Walking

Telephone Number

Running

Student has the same attributes and behaviors as Person because Student is a person. In addition, Student has attributes and behaviors that are unique to a student. Some of these are shown in Table 1-2.

Table 1-2: Attributes and Behaviors of Student

Attributes

Behaviors

Student Number

Taking a test

Major

Attending class

Grade Point Average

Doing homework

It makes sense that Student inherits attributes and behaviors of Person because Student has an is a relationship with Person. That is, a student is a person. You ll learn how to define objects in Chapter 2 and how objects are inherited in Chapter 5. For now, let s consider Table 1-1 as the definition of Person and Table 1-2 as the definition of Student.

Table 1-1 and Table 1-2 simply define Person and Student, but they don t create a real person or real student. For that, you need to create an instance of Person or Student. An instance is a real object. Therefore, an instance of Person is a real person, and an instance of Student is a real student.

Once Student inherits from Person, an instance of Student has the attributes and behaviors of Person and Student, just as if all attributes and behaviors were defined in Student. Table 1-3 shows the attributes and behaviors of an instance of Student after Student inherits from Person.

Table 1-3: Attributes and Behaviors Available to an Instance of Student

Attributes

Behaviors

First Name

Sitting

Last Name

Standing

Address

Walking

Telephone Number

Running

Student Number

Taking a test

Major

Attending class

Grade Point Average

Doing homework

This means that a programmer who creates only an instance of Student can access attributes and behaviors of Person without creating an instance of Person. The programmer accesses attributes and behaviors of Person by using the instance of Student.

Why Use Inheritance?

Maintaining objects can become a nightmare for programmers, as you ll quickly discover once you start creating your own object-oriented programs. Real-world programs require many objects, each having many attributes and behaviors. It becomes your responsibility to organize these objects so you can easily change them as requirements for your program change.

Here s the dilemma: Let s say you don t define a Person object. Instead, attributes and behaviors that would normally be defined in a Person object (refer to Table 1-1) are placed in objects that are persons. These objects are Student, Teacher, Department Chair, Dean, Secretary, and Bursar. In other words, attributes and behaviors listed in Table 1-1 are repeated six times, once for each of the six objects.

Now for the problem: You need to insert a Cell Phone attribute. It makes sense to insert the Cell Phone attribute in the Person object because many people have cell phones, but the Person object isn t defined. The only solution is to insert the Cell Phone attribute into all six objects.

Real-world programmers don t do this because they know to group together into one object those attributes and behaviors that are common to multiple objects. In this example, common attributes and behaviors of a person are defined in the Person object. The other five objects inherit the Person object and its attributes and behaviors.

Inserting the Cell Phone attribute becomes an easy task because the change is made to one object ”the Person object. The other objects automatically receive the Cell Phone attribute when they inherit the Person object.




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

Similar book on Amazon

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