Home OOP Option Last Next


Describe the relationships between objects for a given problem.


Teaching Note:

The relationships that should be known are dependency (“uses”), aggregation (“has a”) and inheritance (“is a”).

LINK Thinking abstractly.

AIM 4 Applying thinking skills critically to decompose scenarios.


Sample Question:


JSR Notes:

Decomposition Leads to Relationships between Classes

In thinking about how to address a larger problem/system with an OOP approach, you first set about decomposing the problem into smaller problems, thereby forming different types/classes. But as you do so, you will start to see that there are specific ways in which the various types/classes releate to each other. OOP classes are seen to have generally three kinds of relationships:


Relationship Concepts


Relationship Diagrams

Here are a couple of diagrams to get you thinking about how these kinds of relationships between classes/objects exist in larger systems.

General Diagram example:

object hierarchy example


UML-style Diagram example:

transit diagram

(JSR Note - when Photoshop correctly licenses, draw in lines with "has a" to Driver from Bus and Tram.)



Relationships In (Java) Code

Dependencey/"uses" is whenever an object of another class is used by a given class. So in a MainGUI class, for example, we will often use a SortAndSearch class, and a SaveAndOpen class; they do work for us, but from another class. Do note why a "uses" relationship is know as a "dependency" - this is because the one will not work without its ability to use the other. And this can be problematic (see next assessment statement).

//"Uses a" example
//Below, MyClass uses an instance of ClassA.

public class MyClass{
    public static void main(String args[]){
        int x = Math.random() * 60;
        int y = Math.random() * 200;
        ClassA a = new ClassA(x, y)

if(x > 30 && y < 100){ System.out.println(a.someClassAMethod()); // this is the "uses" dependency relationship } else{ System.out.println("The criteria were not met."); } } }

Aggregation/"has a" is when we make an instance of one class in another "template class".

//"Has a" example
//Below, this template class includes an instance of another template class as one of its attributes

public class ATemplateClass{

    private String s = "not set yet";
    private int size = -999;
    private OtherClass oc;       
    // this is the "has a" aggregation relationship

    public ATemplateClass(){

    public ATemplateClass(String s, int size, OtherClass oc){
        this.s = s;
        this.size = size;
        this.oc = oc;

    //accessor methods
//modifier methods

Inheritance is when a class inherits ("extends") another. For example, there could be an ISBEmployee class which has attributes of name and address, and all other specialized employee classes, such as ISBTeacher, ISBAssistantTeacher, and ISBAdministrator would inherit these attributes (and their related methods), as well as add others of their own. When inheritance takes place, it is a "is a" relationship; an ISBTeacher is indeed an ISBEmployee, though it does have a few more attributes and actions as well.

//Inheritance example
//Showing that ANewClass is a sub-class of AnotherClass.

public class ISBTeacher extends ISBEmployee{        
// this line, above, shows the "is a" inheritance situation
    private String name;
    private int idNumber;

    public ANewClass(boolean isFemale, String name, int idNumber){
        this.name = name;
        this.idNumber = idNumber;