Docsity

Aggregation of Object-Oriented Programming - Lecture Slides | COP 3330, Study notes of Computer Science

Toggle sidebar
bg1
Aggregation: The HAS-A Relationship
Thus far, most of our examples have involved "simple" classes
in that the instance variables of the classes have all been
primitives.
However, this is not a requirement. In fact, we have already
seen examples where one of the instance variables of a class we
defined was a String, which is not a primitive.
Complex programs however require complex classes, where
the individual components of the class may be other objects.
Conceptually, we can come up with real life examples:
A Car object HAS-A Wheel object. (In fact, it would have four
=))
Note, that this relationship is a different one than the
relationship between a Sports Car and a Car. Namely, A Car is
NOT a Wheel, but a Sports Car IS-A Car.
Another analogy that should make the difference clear is as
follows:
A person HAS-A arm, but a person IS NOT an arm.
A football player IS-A person, but does not HAVE a person.
pf2

Partial preview of the text

Download Aggregation of Object-Oriented Programming - Lecture Slides | COP 3330 and more Study notes Computer Science in PDF only on Docsity!

Aggregation: The HAS-A Relationship

Thus far, most of our examples have involved "simple" classes in that the instance variables of the classes have all been primitives. However, this is not a requirement. In fact, we have already seen examples where one of the instance variables of a class we defined was a String, which is not a primitive. Complex programs however require complex classes, where the individual components of the class may be other objects. Conceptually, we can come up with real life examples: A Car object HAS-A Wheel object. (In fact, it would have four =)) Note, that this relationship is a different one than the relationship between a Sports Car and a Car. Namely, A Car is NOT a Wheel, but a Sports Car IS-A Car. Another analogy that should make the difference clear is as follows: A person HAS-A arm, but a person IS NOT an arm. A football player IS-A person, but does not HAVE a person.

We will focus on two examples for this lecture: (1) A Contact object will store information for one person. An AddressBook object will store many Contact objects, and will maintain this list. (2) We will build a Lemonade class, that allows us to play the game Lemonade. The Lemonade object will comprise the following components: (a) a BankAccount object to keep the money for the game (b) the number of bags of sugar owned (c) the number of bags of lemons owned (d) the price of a single cup of lemonade (e) the name of the Lemonade stand owner In this latter example, the BankAccount object is comprised of the following components: (a) the name of the owner (b) the amount of money in savings (c) the amount of money in checking (d) the interest rate for the savings account In addition, in order to run a game of Lemonade, we'll need to use a Weather object.