0% found this document useful (0 votes)
37 views16 pages

Class Relationship

This document discusses different types of class relationships in object-oriented programming, including aggregation, composition, association, and inheritance. It provides examples to illustrate each type of relationship, such as an association between a person and a company through employment, and an inheritance relationship where specific classes like car and truck inherit from a more general vehicle class. It also compares the differences between aggregation and inheritance, noting that aggregation represents a part-of relationship while inheritance models an is-a relationship between classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views16 pages

Class Relationship

This document discusses different types of class relationships in object-oriented programming, including aggregation, composition, association, and inheritance. It provides examples to illustrate each type of relationship, such as an association between a person and a company through employment, and an inheritance relationship where specific classes like car and truck inherit from a more general vehicle class. It also compares the differences between aggregation and inheritance, noting that aggregation represents a part-of relationship while inheritance models an is-a relationship between classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

EKT472: Object Oriented

Programming
Class Relationship
Example of Class

Sto pW atch

-seconds
+start()
+stop()
+reset()
+getSeconds(): doub le
Associations and Links
 Link is physical or conceptual connection between objects
 Link is an instance of an Association

Example:
Link : Susan is-wife-of Robert
Julie is-wife-of John
Association: Woman is-wife-of Man

Associations and Links appear as Verbs


Associations & Links...

 Associations are bi-directional

may be implemented as
unidirectional
 Implemented usually as pointers
 important not to think as pointers
 Associations may be

one-to-one
one-to-many
many-to-one
many-to-many
Associations Notations

Ex ac tly One
* M a ny
0 ..1Optiona l
Directed Association
*
1 ..* One Or M ore
0 ..* Ze ro Or M ore

0..* 1..*
Co mpa ny emp loyees Person
emp loys wo rksFor
Link Attributes & Association Classes
 Attributes that belong to association of object rather than one object

 Link Attributes belong in Association Classes

Ex: Salary received by Employee from Company


 In an one-to-one association you may try to make it attribute of one of the
objects

Leads to extensibility problems


Association Class
Notation

* *
Person Company

Employment
salary:double
Role Names
 Name given to either end of an association

• Helps to navigate from one object to


related objects
wife husband
Woman 0..1 married 0..1 Man

• Helps clarify when two classes have


several associations between them
wife husband
0..1 married 0..1
Woman 1
Man
*
daughter children father
Aggregation & Composition

Aggregation:

Part-of or part-whole relationship (by reference)


Example : Car has Engine and Transmission
Assembly of objects with aggregate and
component parts
Component existence may or may not depend
on aggregate
Aggregation & Composition...

Composition:

Part belongs to only one whole (by Value)


Part lives and dies with the whole
Whole cannot replace the part
Aggregation vs Composition Example

Person Composition
brain

Aggregation

Car
Aggregation Vs. Association
 Special form of Association
 Confusing
 Aggregation represents “part-of” relationship
 Some operations on whole automatically applied to its parts
 Aggregate is asymmetric : part is subordinate to the whole
 Association is symmetric : objects involved are of equal stature
Inheritance
 Models is-a relationship
 Relationship between a class and its refined versions
 Superclass or Base class
 Subclass or Derived class
 Inheritance is transitive
 Discriminator : The property being abstracted by a particular inheritance
 Breath Vs. Depth of inheritance
Inheritance Notation
Generalization

Superclass
Vehicle

+dri ve()
Subclass1 Subclass2

Ca r Tru ck

-sun Roo f
Inheritance Example

Child Animal

Girl Boy Dog Cat


Aggregation vs. Inheritance
 Aggregation represents part-of relationship

 Inheritance represents kind-of relationship

 Aggregation refers to object relationships

 Inheritance refers to class relationships

You might also like