Object Relations
Object Relations
Relation Name | Verbal Expression |
---|---|
Association | Has reference to a |
Aggregation | Has reference to a |
Composition | Has ownership of a |
Inheritance | Is a |
Dependence | Uses a |
Association
(“has-reference-to-a”)
- Indicates that a class holds a reference to another instance of a class.
- In Java, it can be implemented through the use of an instance field.
- Can be bi-directional with each class holding a reference to the other.
- Is achieved through the more specific associative relations:
- Aggregation
- Composition
Aggregation
“has-a” or “has many”
- restricted form of association relations.
- denotes that an object has ownership of another object
- Each object referenced is said to be a child of the referencer (parent).
- In Java, can be implemented through the use of an instance field.
- Is a one-way (unidirectional) relationship
- The degree of cardinality can be
- one to one (
1-1
) - one to many (
1-M
)
- one to one (
Aggregation (“has-a”)
Example
- Express that a
Person
has apet
using aggregation.
public class Person {
private Animal pet;
}
Aggregation (“has-many”)
Example 1
- Express that a
Person
has manypets
using aggregation.
public class Person {
private Animal[] pets;
}
Aggregation (“has-many”)
Example 2
- Express that a
Person
has manypets
using aggregation.
public class Person {
private List<Animal> pets;
}
Composition (“has-a”)
- restricted form of aggregation relations.
- denotes that an object is responsible for the creation AND destruction of another object.
- when
this
object is garbage collected, its composite children are also garbage collected
- when
- The degree of cardinality can be
- one to one (
1-1
) - one to many (
1-M
)
- one to one (
Composition (“has-a”)
Example
- Express that a
Person
has abirthDate
using composition.
public class Person {
private GregorianCalendar birthDate;
public Person(int day, int month, int year) {
this.birthDate = new GregorianCalendar(year, month, day);
}
}
Dependence
(“uses-a”)
- Dependence describes a “uses-a” relationship.
- Typically implemented in java by ensuring a method consumes a parameter-type of the dependee.
Dependence
(“uses-a”)
Example
- Express that a
Grader
uses aStudent
to obtain aCharacter
representative of a grade.
public class Grader {
public Character grade(Student student) {
// ... definition ommitted for brevity ...
}
}
Inheritance (“is-a”)
- This relationship denotes dynamic-polymorphism.
- denotes that a class is a specific type of a more general super class.
- denotes a class has inherent-members (methods / variables) from super class
- inherent-members are not explicitly declared in the inheriting class (sub class)
- All classes are implicitly sub-classes of the
Object
class.
Inheritance (“is-a”)
Example
- Express that a
Student
is aPerson
with a grade represented as aCharacter
public class Person {
private String name;
private Date birthDate;
public Person(String name, Date birthDate) {
this.name = name;
this.birthDate = birthDate;
} // getters and setter ommitted for brevity
}
public class Student extends Person {
private Character currentGrade;
public Student(String name, Date birthDate, Character initialGrade) {
super(name, birthDate);
this.currentGrade = initialGrade;
} // getters and setter ommitted for brevity
}