SlideShare a Scribd company logo
1 of 41
Working with Classes and Their
Relationships

1
Understanding Class Compositions
and Associations

• Exam Objective 1.3 Describe, compare, and contrast
class compositions, and associations (including
multiplicity: one-to-one, one-to-many, and many-to-many),
and association navigation.

2
• Class compositions and associations
• Class relationships
• Multiplicities
• Association navigation

3
Class Compositions and Associations
• Composition and association are the general terms used
to describe a class relationship.
• An association or composition relationship is formed
between two objects when one contains a reference to
the other. The reference is often stored as an instance
variable.

• The reference may be in one direction or bidirectional.

4
• An association relationship is a relationship of two objects where
neither one directly depends on the other for their logical
meaning.
• For example, object A has an association relationship with object
B. If this relationship was lost, both objects would still retain the
same meaning they previously had.
• These relationships are considered weak. Objects in an
association relationship have no dependence on each other for
the management of their life cycle.
• In other words, the existence of an object is not tied to the
existence of the other object in the relationship.
• The CarFactory object and CarFrame object have an association
relationship. If this relationship no longer existed, each object
could continue to logically make sense and retain its original
meaning on its own.
5
• Composition means that one object is composed of another.
• The existence of object B is directly tied to the existence of object
A. When object A no longer exists, object B would also no longer
exist.
• A Car object and CarStatus object would be an example of a
composition relationship.
• The Car object is composed-of the CarStatus object. Both objects
depend on this relationship to define their meanings.
• The CarStatus object also depends on the Car object to maintain
its life cycle. When the Car object no longer exists, the CarStatus
object would also no longer exist.

6
A composition will always be responsible for an object’s life cycle.

Composition relationships also represent a stronger relationship compared to an association.
Objects belonging to an association make more sense by themselves than objects of composition.
7
Class Relationships

•Direct association
•Composition association
•Aggregation association
•Temporary association
8
Direct Association
• Direct association describes a “has-a” relationship.
This is a basic association that represents
navigability.
• Direct association is a weak relationship and
therefore can be generalized to an association.
• There is no life cycle responsibility and each object
• in the relationship can conceptually be independent.

9
Composition Association
• A composition association can be described as object A is
“composed-of” object B.
• The containing object also has the responsibility of
managing the life cycle of the internal object.
• It is possible for this object to pass the life cycle
management to another object.
• Life cycle management means that the object composed
of the second object, or the containing object, must
maintain a reference to the inner object, otherwise the
Java Virtual Machine will destroy it.
• If the containing object is destroyed, any objects that
compose it will also be destroyed.
10
Aggregation Association
• An aggregation association is a relationship that
represents one object being part of another object.
• Neither object depends on the other for its existence.

11
Temporary Association
• Temporary association is also known as a dependency.
• Typically, a temporary association will be an object used as a
local variable, return value, or method parameter.
• For example, a Car object may have a method called
startEngine that has a Key object as a parameter.
• The Key object as a parameter would represent a temporary
association.

12
13
MULTIPLICITIES

14
One-to-One Multiplicity.
• All four relationship types may have a one-to-one
multiplicity.

15
One-to-Many Multiplicity
• One-to-many relationships are created when one object
contains a reference to a group of like objects.
• The multiple object references are normally stored in an
array or a collection.
• All four relationship types may be one-to-many.
• A many-to-one relationship is also possible.

16
Many-to-Many Multiplicity
• Many-to-many relationships are only possible for aggregation
associations, direct associations, and temporary associations.
• Composition association is a strong relationship that implies a
life cycle responsibility for the object that composes it.

17
Association Navigation
• Association navigation is a term used to describe the
direction in which a relationship can be traveled.
• An object that is contained within another object is said to be
navigable if the containing object has methods for accessing
the inner object.
• Most relationships are navigable in one direction, but if both
objects contain references to the other, it is possible to have
a bidirectional navigable relationship.

18
Association Navigation

19
Class Compositions and Associations in
Practice
• Exam Objective 3.3 Develop code that implements simple
class associations, code that implements multiplicity using
arrays, and recognize code that implements compositions
as opposed to simple associations, and code that
correctly implements association navigation.

20
One-to-One Class Association
public class Truck {
/* This is an example of a one-to-one
direct association */
Trailer trailer;
void setTrailer(Trailer t){
trailer = t;
}
/*
* Remainder of Truck class would be here */
}

21
One-to-Many Class Association
public class Car {
Wheel[] wheel = new Wheel[4];
void setWheels(Wheel w) {
wheel[0] = w;
wheel[1] = w;
wheel[2] = w;
wheel[3] = w;
}
// Remainder of Car class would be here
}

22
Many-to-Many Class Association
• An array or collection should be a dead give away
that you are looking at a *-to-many relationship.
• If there is only one object and an array or collection, it
will be a one-to-many relationship.
• If there are two arrays or collections with references
to each other, it will be a many-to-many relationship.

23
One-to-One Class Composition
public class Tire {
TireAirPressure tireAirPressure;
Tire(){
tireAirPressure = new TireAirPressure();
}
}
• The Tire object has life cycle management responsibilities
• to the TireAirPressure object.
• If the Tire object was destroyed, the TireAirPressure object
would also be destroyed.

24
One-to-Many Class Composition
public class SensorStatus {
int status;
public SensorStatus(int
newStatus) {
status = newStatus;
}
}

public class CarComputer {
SensorStatus[] sensorStatus = new
SensorStatus[5];
public CarComputer() {
sensorStatus[0] = new SensorStatus(1);
sensorStatus[1] = new SensorStatus(1);
sensorStatus[2] = new SensorStatus(1);
sensorStatus[3] = new SensorStatus(1);
sensorStatus[4] = new SensorStatus(1);
}
}
25
Examples of Association Navigation
public class PinStripe {
Color color = new Color(Color.blue);
Color getColor(){
return color;
}
}
• any object that had access to the PinStripe object could use its
getColor method, which is considered a getter, to navigate to
the Color object.
• In this example, the navigation is only in a single direction.
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

More Related Content

Viewers also liked

Chapter 9:Representing Object-Oriented Concepts with UML
Chapter 9:Representing Object-Oriented Concepts with UMLChapter 9:Representing Object-Oriented Concepts with UML
Chapter 9:Representing Object-Oriented Concepts with UMLIt Academy
 
Chapter 12:Understanding Server-Side Technologies
Chapter 12:Understanding Server-Side TechnologiesChapter 12:Understanding Server-Side Technologies
Chapter 12:Understanding Server-Side TechnologiesIt Academy
 
Chapter 5:Understanding Variable Scope and Class Construction
Chapter 5:Understanding Variable Scope and Class ConstructionChapter 5:Understanding Variable Scope and Class Construction
Chapter 5:Understanding Variable Scope and Class ConstructionIt Academy
 
chap4 : Converting and Casting (scjp/ocjp)
chap4 : Converting and Casting (scjp/ocjp)chap4 : Converting and Casting (scjp/ocjp)
chap4 : Converting and Casting (scjp/ocjp)It Academy
 
Chapter 10:Understanding Java Related Platforms and Integration Technologies
Chapter 10:Understanding Java Related Platforms and Integration TechnologiesChapter 10:Understanding Java Related Platforms and Integration Technologies
Chapter 10:Understanding Java Related Platforms and Integration TechnologiesIt Academy
 
Chapter 3 : Programming with Java Operators and Strings
Chapter 3 : Programming with Java Operators and  StringsChapter 3 : Programming with Java Operators and  Strings
Chapter 3 : Programming with Java Operators and StringsIt Academy
 
Chapter 7:Understanding Class Inheritance
Chapter 7:Understanding Class InheritanceChapter 7:Understanding Class Inheritance
Chapter 7:Understanding Class InheritanceIt Academy
 
Chapter 3:Programming with Java Operators and Strings
Chapter 3:Programming with Java Operators and  StringsChapter 3:Programming with Java Operators and  Strings
Chapter 3:Programming with Java Operators and StringsIt Academy
 

Viewers also liked (12)

Chapter 9:Representing Object-Oriented Concepts with UML
Chapter 9:Representing Object-Oriented Concepts with UMLChapter 9:Representing Object-Oriented Concepts with UML
Chapter 9:Representing Object-Oriented Concepts with UML
 
Chapter 12:Understanding Server-Side Technologies
Chapter 12:Understanding Server-Side TechnologiesChapter 12:Understanding Server-Side Technologies
Chapter 12:Understanding Server-Side Technologies
 
Dunya gokyuzu
Dunya gokyuzuDunya gokyuzu
Dunya gokyuzu
 
Chapter 1 :
Chapter 1 : Chapter 1 :
Chapter 1 :
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Chapter 5:Understanding Variable Scope and Class Construction
Chapter 5:Understanding Variable Scope and Class ConstructionChapter 5:Understanding Variable Scope and Class Construction
Chapter 5:Understanding Variable Scope and Class Construction
 
3 boyutlu cisimler
3 boyutlu cisimler3 boyutlu cisimler
3 boyutlu cisimler
 
chap4 : Converting and Casting (scjp/ocjp)
chap4 : Converting and Casting (scjp/ocjp)chap4 : Converting and Casting (scjp/ocjp)
chap4 : Converting and Casting (scjp/ocjp)
 
Chapter 10:Understanding Java Related Platforms and Integration Technologies
Chapter 10:Understanding Java Related Platforms and Integration TechnologiesChapter 10:Understanding Java Related Platforms and Integration Technologies
Chapter 10:Understanding Java Related Platforms and Integration Technologies
 
Chapter 3 : Programming with Java Operators and Strings
Chapter 3 : Programming with Java Operators and  StringsChapter 3 : Programming with Java Operators and  Strings
Chapter 3 : Programming with Java Operators and Strings
 
Chapter 7:Understanding Class Inheritance
Chapter 7:Understanding Class InheritanceChapter 7:Understanding Class Inheritance
Chapter 7:Understanding Class Inheritance
 
Chapter 3:Programming with Java Operators and Strings
Chapter 3:Programming with Java Operators and  StringsChapter 3:Programming with Java Operators and  Strings
Chapter 3:Programming with Java Operators and Strings
 

Similar to Chapter 6:Working with Classes and Their Relationships

Similar to Chapter 6:Working with Classes and Their Relationships (20)

UML-class diagram for beginners to adance.ppt
UML-class diagram for beginners to adance.pptUML-class diagram for beginners to adance.ppt
UML-class diagram for beginners to adance.ppt
 
UML-class_diagram.ppt
UML-class_diagram.pptUML-class_diagram.ppt
UML-class_diagram.ppt
 
Design pattern - part 1
Design pattern - part 1Design pattern - part 1
Design pattern - part 1
 
Association agggregation and composition
Association agggregation and compositionAssociation agggregation and composition
Association agggregation and composition
 
Domain model Refinement
Domain model RefinementDomain model Refinement
Domain model Refinement
 
Unit iv dbms
Unit   iv dbmsUnit   iv dbms
Unit iv dbms
 
Ooad ch 4
Ooad ch 4Ooad ch 4
Ooad ch 4
 
06 class diagrams
06 class diagrams06 class diagrams
06 class diagrams
 
Introduction to UML
Introduction to UMLIntroduction to UML
Introduction to UML
 
UML Training for Business Analysts
UML Training for Business AnalystsUML Training for Business Analysts
UML Training for Business Analysts
 
E_R-Diagram (2).pptx
E_R-Diagram (2).pptxE_R-Diagram (2).pptx
E_R-Diagram (2).pptx
 
Icom4015 lecture11-s16
Icom4015 lecture11-s16Icom4015 lecture11-s16
Icom4015 lecture11-s16
 
Introduction to OOA and UML
Introduction to OOA and UMLIntroduction to OOA and UML
Introduction to OOA and UML
 
Introduction to OOA and UML
Introduction to OOA and UMLIntroduction to OOA and UML
Introduction to OOA and UML
 
ER-Model-ER Diagram
ER-Model-ER DiagramER-Model-ER Diagram
ER-Model-ER Diagram
 
Association
AssociationAssociation
Association
 
PROPERTIES OF RELATIONSHIPS AMONG OBJECTS IN OBJECT-ORIENTED SOFTWARE DESIGN
PROPERTIES OF RELATIONSHIPS AMONG OBJECTS IN OBJECT-ORIENTED SOFTWARE DESIGNPROPERTIES OF RELATIONSHIPS AMONG OBJECTS IN OBJECT-ORIENTED SOFTWARE DESIGN
PROPERTIES OF RELATIONSHIPS AMONG OBJECTS IN OBJECT-ORIENTED SOFTWARE DESIGN
 
Er model
Er modelEr model
Er model
 
Intro to UML
Intro to UMLIntro to UML
Intro to UML
 
Database design
Database designDatabase design
Database design
 

More from It Academy

Chapter 4:Object-Oriented Basic Concepts
Chapter 4:Object-Oriented Basic ConceptsChapter 4:Object-Oriented Basic Concepts
Chapter 4:Object-Oriented Basic ConceptsIt Academy
 
Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsIt Academy
 
chap 10 : Development (scjp/ocjp)
chap 10 : Development (scjp/ocjp)chap 10 : Development (scjp/ocjp)
chap 10 : Development (scjp/ocjp)It Academy
 
chap 6 : Objects and classes (scjp/ocjp)
chap 6 : Objects and classes (scjp/ocjp)chap 6 : Objects and classes (scjp/ocjp)
chap 6 : Objects and classes (scjp/ocjp)It Academy
 
chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)It Academy
 
Chap1 language fondamentale of java ( scjp /ocjp)
Chap1 language fondamentale of java ( scjp /ocjp)Chap1 language fondamentale of java ( scjp /ocjp)
Chap1 language fondamentale of java ( scjp /ocjp)It Academy
 
Design patterns gof fr
Design patterns gof frDesign patterns gof fr
Design patterns gof frIt Academy
 

More from It Academy (7)

Chapter 4:Object-Oriented Basic Concepts
Chapter 4:Object-Oriented Basic ConceptsChapter 4:Object-Oriented Basic Concepts
Chapter 4:Object-Oriented Basic Concepts
 
Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java Statements
 
chap 10 : Development (scjp/ocjp)
chap 10 : Development (scjp/ocjp)chap 10 : Development (scjp/ocjp)
chap 10 : Development (scjp/ocjp)
 
chap 6 : Objects and classes (scjp/ocjp)
chap 6 : Objects and classes (scjp/ocjp)chap 6 : Objects and classes (scjp/ocjp)
chap 6 : Objects and classes (scjp/ocjp)
 
chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)
 
Chap1 language fondamentale of java ( scjp /ocjp)
Chap1 language fondamentale of java ( scjp /ocjp)Chap1 language fondamentale of java ( scjp /ocjp)
Chap1 language fondamentale of java ( scjp /ocjp)
 
Design patterns gof fr
Design patterns gof frDesign patterns gof fr
Design patterns gof fr
 

Recently uploaded

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 

Recently uploaded (20)

New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 

Chapter 6:Working with Classes and Their Relationships

  • 1. Working with Classes and Their Relationships 1
  • 2. Understanding Class Compositions and Associations • Exam Objective 1.3 Describe, compare, and contrast class compositions, and associations (including multiplicity: one-to-one, one-to-many, and many-to-many), and association navigation. 2
  • 3. • Class compositions and associations • Class relationships • Multiplicities • Association navigation 3
  • 4. Class Compositions and Associations • Composition and association are the general terms used to describe a class relationship. • An association or composition relationship is formed between two objects when one contains a reference to the other. The reference is often stored as an instance variable. • The reference may be in one direction or bidirectional. 4
  • 5. • An association relationship is a relationship of two objects where neither one directly depends on the other for their logical meaning. • For example, object A has an association relationship with object B. If this relationship was lost, both objects would still retain the same meaning they previously had. • These relationships are considered weak. Objects in an association relationship have no dependence on each other for the management of their life cycle. • In other words, the existence of an object is not tied to the existence of the other object in the relationship. • The CarFactory object and CarFrame object have an association relationship. If this relationship no longer existed, each object could continue to logically make sense and retain its original meaning on its own. 5
  • 6. • Composition means that one object is composed of another. • The existence of object B is directly tied to the existence of object A. When object A no longer exists, object B would also no longer exist. • A Car object and CarStatus object would be an example of a composition relationship. • The Car object is composed-of the CarStatus object. Both objects depend on this relationship to define their meanings. • The CarStatus object also depends on the Car object to maintain its life cycle. When the Car object no longer exists, the CarStatus object would also no longer exist. 6
  • 7. A composition will always be responsible for an object’s life cycle. Composition relationships also represent a stronger relationship compared to an association. Objects belonging to an association make more sense by themselves than objects of composition. 7
  • 8. Class Relationships •Direct association •Composition association •Aggregation association •Temporary association 8
  • 9. Direct Association • Direct association describes a “has-a” relationship. This is a basic association that represents navigability. • Direct association is a weak relationship and therefore can be generalized to an association. • There is no life cycle responsibility and each object • in the relationship can conceptually be independent. 9
  • 10. Composition Association • A composition association can be described as object A is “composed-of” object B. • The containing object also has the responsibility of managing the life cycle of the internal object. • It is possible for this object to pass the life cycle management to another object. • Life cycle management means that the object composed of the second object, or the containing object, must maintain a reference to the inner object, otherwise the Java Virtual Machine will destroy it. • If the containing object is destroyed, any objects that compose it will also be destroyed. 10
  • 11. Aggregation Association • An aggregation association is a relationship that represents one object being part of another object. • Neither object depends on the other for its existence. 11
  • 12. Temporary Association • Temporary association is also known as a dependency. • Typically, a temporary association will be an object used as a local variable, return value, or method parameter. • For example, a Car object may have a method called startEngine that has a Key object as a parameter. • The Key object as a parameter would represent a temporary association. 12
  • 13. 13
  • 15. One-to-One Multiplicity. • All four relationship types may have a one-to-one multiplicity. 15
  • 16. One-to-Many Multiplicity • One-to-many relationships are created when one object contains a reference to a group of like objects. • The multiple object references are normally stored in an array or a collection. • All four relationship types may be one-to-many. • A many-to-one relationship is also possible. 16
  • 17. Many-to-Many Multiplicity • Many-to-many relationships are only possible for aggregation associations, direct associations, and temporary associations. • Composition association is a strong relationship that implies a life cycle responsibility for the object that composes it. 17
  • 18. Association Navigation • Association navigation is a term used to describe the direction in which a relationship can be traveled. • An object that is contained within another object is said to be navigable if the containing object has methods for accessing the inner object. • Most relationships are navigable in one direction, but if both objects contain references to the other, it is possible to have a bidirectional navigable relationship. 18
  • 20. Class Compositions and Associations in Practice • Exam Objective 3.3 Develop code that implements simple class associations, code that implements multiplicity using arrays, and recognize code that implements compositions as opposed to simple associations, and code that correctly implements association navigation. 20
  • 21. One-to-One Class Association public class Truck { /* This is an example of a one-to-one direct association */ Trailer trailer; void setTrailer(Trailer t){ trailer = t; } /* * Remainder of Truck class would be here */ } 21
  • 22. One-to-Many Class Association public class Car { Wheel[] wheel = new Wheel[4]; void setWheels(Wheel w) { wheel[0] = w; wheel[1] = w; wheel[2] = w; wheel[3] = w; } // Remainder of Car class would be here } 22
  • 23. Many-to-Many Class Association • An array or collection should be a dead give away that you are looking at a *-to-many relationship. • If there is only one object and an array or collection, it will be a one-to-many relationship. • If there are two arrays or collections with references to each other, it will be a many-to-many relationship. 23
  • 24. One-to-One Class Composition public class Tire { TireAirPressure tireAirPressure; Tire(){ tireAirPressure = new TireAirPressure(); } } • The Tire object has life cycle management responsibilities • to the TireAirPressure object. • If the Tire object was destroyed, the TireAirPressure object would also be destroyed. 24
  • 25. One-to-Many Class Composition public class SensorStatus { int status; public SensorStatus(int newStatus) { status = newStatus; } } public class CarComputer { SensorStatus[] sensorStatus = new SensorStatus[5]; public CarComputer() { sensorStatus[0] = new SensorStatus(1); sensorStatus[1] = new SensorStatus(1); sensorStatus[2] = new SensorStatus(1); sensorStatus[3] = new SensorStatus(1); sensorStatus[4] = new SensorStatus(1); } } 25
  • 26. Examples of Association Navigation public class PinStripe { Color color = new Color(Color.blue); Color getColor(){ return color; } } • any object that had access to the PinStripe object could use its getColor method, which is considered a getter, to navigate to the Color object. • In this example, the navigation is only in a single direction. 26
  • 27. 27
  • 28. 28
  • 29. 29
  • 30. 30
  • 31. 31
  • 32. 32
  • 33. 33
  • 34. 34
  • 35. 35
  • 36. 36
  • 37. 37
  • 38. 38
  • 39. 39
  • 40. 40
  • 41. 41