INTRO TO COMPUTER SCIENCE II
OBJECT RELATIONSHIPS
CS162
Relationships between Objects
What kind of objects have we made so far?
How are they related?
Two main types
Object Composition & Inheritance
Composition
“part-of” relationship
Complex object (class) is built from one or more simpler
Heart is a part of you
objects (parts/members)
Part’s (member’s) existence managed by object (class) Responsible for creation/destruction of parts
Part’s lifetime is bound to the lifetime of it’s object
Part (member) can only belong to one object (class) at a time Typically seen with normal member variables
Part (member) doesn’t know about the object (class) Unidirectional relationship
X and Y are part of a Point A Card is a part of a Deck
Aggregation
“has-a” relationship
Complex object (class) is built from one or more simpler objects
You have an address
Roommate has the (same) address
(parts/members)
Part’s (member’s) existence is not managed by object (class) Not responsible for creation/destruction of parts
They are created outside the scope of the class
Part (member) can belong to multiple objects (classes) at a time Typically seen with references or pointer member variables
Passed in as constructor parameter, or set later
Part (member) doesn’t know about the object (class) Unidirectional relationship
Which relationship type?
A ball that has a color
An employer that is employing multiple people The departments in a university
Your age and you
A bag of marbles
Which relationship type?
A ball that has a color
Composition – Color is an intrinsic property of a ball
An employer that is employing multiple people
Aggregation – An employer doesn’t start with any employees and hopefully doesn’t
destroy all its employees when it goes bankrupt The departments in a university
Composition – departments can’t exist in absence of a university Your age and you
Composition – Your age is an intrinsic property of you
A bag of marbles
Aggregation – the bag and the marbles have independent existences
Association
“uses-a” relationship (not part/whole)
Objects are otherwise unrelated
Members existence is not managed by class Not responsible for creating/destroying parts
Doctor uses patient (for income) Patient uses doctor (for health)
Members can belong to multiple classes
Typically use pointer members that point to object
outside scope of aggregate class
Members may or may not know about the object (class)
Unidirectional or bidirectional
Inheritance
“is-a” relationship
Create complex objects by directly inheriting the attributes and behaviors of other objects and then specializing them
Class being inherited from is known as parent, base, or superclass
Class inheriting is known as child, derived, or subclass
Helps avoid re-inventing the wheel (duplicate code)
Inheritance
Uses hierarchies to show how objects are related
Two common types Progression
Over time
Categorization
From general to specific
Adult
Baby
Child
Inheritance
Uses hierarchies to show how objects are related
Two common types Progression
Over time
Categorization
From general to specific
Adult
Shape Triangle
Baby
Child
Quadrilateral Rectangle
Inheritance
Suppose that we want to implement two C++ classes with the following member variables
Student
Name
ID number
Email address Phone number GPA
Major
Minor
Teacher Name
ID number
Email address Phone number Office number Office Hours
Department
Inheritance
If Student and Teacher are both derived classes, we don’t have to write the same code twice
Can add a Person class to hold any redundant information
Person
•Name
•Email
•ID
•Phone number
Student
• GPA
• Major
• Minor
Teacher
• Office number • Office hours
• Department
Inheritance
Not limited to a single level Inheritance chains
Student
• GPA
• Major
• Minor
Person
•Name
•Email
•ID
•Phone number
Employee
• Department • Salary
Teacher
• Office number
• Office Hours
Staff
• Duties
Composition vs. Inheritance
Game Players
Deck
Animal
Canine Dog
Wolf