Chapter 9. Building Objects

 <  Day Day Up  >  

The previous two chapters cover the topics of inheritance and composition. In Chapter 7, "Mastering Inheritance and Composition," we learned that inheritance and composition represent the primary ways to build objects. In Chapter 8, "Frameworks and Reuse: Designing with Interfaces and Abstract Classes," we learned that there are varying degrees of inheritance and how inheritance, interfaces, abstract classes, and composition all fit together.

This chapter covers the issue of how objects are related to each other in an overall design. You might say that this topic was already introduced, and you would be correct. Both inheritance and composition represent ways that objects interact. However, inheritance and composition have one significant difference in the way objects are built. When inheritance is used, the end result is, at least conceptually, a single class that incorporates all of the behaviors and attributes of the inheritance hierarchy. When composition is used, several different classes are used to build the target class.

Although it is true that inheritance is a relationship between two classes, what is really happening is that a wholly new class is created. Let's revisit the example of the Person and Employee classes (see Figure 9.1).

Figure 9.1. An inheritance relationship.

graphics/09fig01.gif

Although there are indeed two classes here, the relationship is not interaction ”it is inheritance. Basically, an employee is a person. An Employee object does not send a message to a Person object. An Employee object does need the services of a Person object. This is because an Employee object is a Person object.

However, composition is a different situation. Composition represents an interaction between distinct objects. So, although Chapter 8 primarily covers the different flavors of inheritance, this chapter delves into the various flavors of composition and how objects interact with each other.

 <  Day Day Up  >  


Object-Oriented Thought Process
Object-Oriented Thought Process, The (3rd Edition)
ISBN: 0672330164
EAN: 2147483647
Year: 2003
Pages: 164
Authors: Matt Weisfeld

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