CS 1302 – Chapter 10
Relationships between Classes
10.1 – Introduction
1.In the previous chapter, we discussed how a class can be used to model a real world object and how to write aclass. the motivation for using a class to model a real world object, and how to implement a class with code.Next, we consider relationships between classes. For example:“A person has a dog”“A person has a bunch of dogs”“A bank has any number of accounts”“A team has a bunch of players”“A team has 5 starters and up to 8 on the bench”“A hotel has some number of rooms”2.There are three types of relationships between classes:RelationshipExampleOne-to-one“A person has a dog”One-to-many“A person has a bunch of dogs”Many-to-many“Each student has some number of courses and each course has some number of students”10.2 – One-to-One Relationships10.2.1 – One Way Navigability1.In a class diagram, we model the one-to-one relationship between two classes with an association which is asolid line drawn between two classes. An association is also called the has-a relationship: A person
dog.
2.When we say a “person has-a dog”, in terms of implementation, we mean that a Person has a Dog instancevariable. In the example below, a Person must have a Dogas a Dog instance is required when the Person created. publicclassPerson {privateString nameprivateDog dogpublicPerson(String name, Dog dog) {this.name = namethis.dog = dog}publicDog getDog() { returndog; }publicString getName() { returnpublicclassDog {privateString namepublicDog(String name) {this.name = name}publicString getName() {returnname;}}
is
;
;
;
;
;
;
1

name
; }
}
3.
In the example above a person knows who her dog is (because of the
dog
instance variable in
Person
), but the
dog does not know who its owner is because there is no instance variable of type
Person
in the
Dog class.
This
illustrates
one-way navigability.
We consider two-way navigability in the next section.
4.
To use the classes above, we could write code as shown below. We
could depict this in an object diagram as shown on the right.
Dog
d
=
new
Dog(
"Spot"
);
Person
p
=
new
Person(
"Leah"
,
d
);
Dog
d2
=
p
.getDog();
5.
Notice in the
Person
class above, we provide a
getDog
method
public
Dog getDog() {
return
dog
;
}
If we want to allow a
Person
to change their
Dog
, we could supply a setter:
public
void
setDog(Dog
dog
) {
this
.
dog
=
dog
;
}
We can use this method with code like this:
Dog
d
=
new
Dog(
"Spot"
);
Person
p
=
new
Person(
"Leah"
,
d
);
Dog
d2
=
new
Dog(
"Chaps"
);
p
.setDog(
d2
);
6.
If we don’t want to require that a
Person
have a
Dog
, then we can add another constructor:
public
Person(String
name
) {
this
(
name
,
null
);
}
Of course, we would need to be careful, because the
getDog
method could return
null.
For example, this code
would generate a runtime error:
Person
p
=
new
Person(
"Leah"
);
Dog
d
=
p
.getDog();
System.
out
.println(
d
.toString());
7.
The class diagram representing these two classes is
shown on the right. Note the following:
This relationship is
one-to-one
. This means that
each
Person
has exactly one
Dog
and each
Dog
can be associated with exactly one
Person
.
2

