Logout

D.1.6

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:

sdfsdfsf

JSR Notes:


Refer to the D.1.4 UML example.

First, here are a couple of diagrams that will help you think about what "uses", "has a" and "is a" mean.

BUT MAKE SURE TO KNOW THE TERMS: dependency, aggregation, and inheritance AS WELL.

object hierarchy example

In the example above, and the examle below, the one above does a better job at showing a class that would be
used by others - you could see how you could directly translate Player into a class which was used by the other
classes.

transit diagram

 

 

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 try to remember that a "uses" relationship is know as a "dependency" - this is because the one will not work without its ability to use the other.

Aggregation/"has a" is when we make an instance of one class in another "template class". So making a Student instance in a MainGUI is an example of this.

So the difference between "uses" and "has a" relationship as seen in code:

"Uses": an instance of another class is made and at least one of its methods isused.


//"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 ca = new ClassA(x, y)

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

 

"Has a": here an instance of another class is one of the "core" attributes of a template class is one


//"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 OtherTemplateClass otc;       // here is the "has a" relationship

    public ATemplateClass(){
    }

    public ATemplateClass(String s, OtherTemplateClass otc){
        this.s = s;
        this.otc = otc;
    }

    //accessor methods
    // ...


    //modifier methods
    // ....
}
   

Inheritance is just that; when a class inherits ("extends") another. For example, there could be an ISPEmployee class which has attributes of name and address, and all other specialized employee classes, such as Teacher, AssistantTeacher, and Administrator would inherit these attributes (and their related methods), and add others of their own. But it is good to note that when inheritance takes place, it is a "is a" relationship. A Teacher is indeed an ISPEmployee, only has a few more attributes and actions.

A full coding example will have to wait, but for now, we could explicity show that a new class inherits from the "mother of all" Java classes, Object. We don't have to do this since all classes inherit from Object, but we can without error.


//Inheritance example
//Showing explicitly the fact that it, like all Java classes, inherits from the Object class

public class ANewClass extends Object{        // this line shows the inheritance situation
    private int a;
    private String b;

    //etc.
}