Relationship between classes.

Inheritance (or Generalization):

This is a representation of the “IS-A” relationship. This relationship is denoted by a solid line with a hollow arrowhead that points from the child element to the parent element. This is relevant to the “extends” keyword in the JAVA.

Screenshot from 2014-02-20 16:11:43

eg : SubClass1 extends SuperClass and SubClass2 extends SuperClass so both SubClass1 and SubClass2 are specializations of SuperClass.

.

.

When it is used too represent the relationship between class and an interface, a class a dashed line with a hollow arrowhead that points from the child element to the parent element will be used. So, in this case it represents the “implements” keyword.

Screenshot from 2014-02-21 09:30:05

eg : Professor implements Person and Student implements Person.

.

.

.

.

.

Association

Relationship between two objects is referred as association.association vs composition vs aggregation Association is denoted by simple arrow (or a single line).

Number of instances can be associated with other, can be denoted by 0,1,n .. * notations.

Both Composition and Aggregation are form of association between two objects, but there is subtle difference between composition and aggregation

Aggregation:

If the including object can exists without being part of main object the relationship os called as aggregation.
An association is known as aggregation when one object uses other object (not owns).

Screenshot from 2014-02-20 16:12:27eg:
Player which is part of a Team, can exists without team and can become part of other teams as well
Student in School class, when School closed, Student still exist and then can join another School.

Aggregation is denoted by an empty diamond

In java (or OOP), this is denoted as the “HAS-A” relationship between two classes. If one object just has another object e.g. Car has driver than it’s Aggregation.

Here, In the image shown, Class2 is part of Class1.Objects of Class1 and Class2 have separate lifetimes.

Exemplary Java code :

public class Organization {
    private List employees;
}

public class Person {
    private String name;   
}

Composition:

When one class owns other class and other class can not meaningfully exist, when it’s owner destroyed the relationship is called as composition.
So, an association is known as composition when one object owns other.

Screenshot from 2014-02-20 16:12:39eg :
Human class is composition of several body parts including Hand, Leg and Heart. When human object dies, all it’s body part ceased to exist meaningfully
Car and it’s part e.g. engines, wheels etc. Individual parts of car can not function, when car is destroyed

Composition is denoted by a filled diamond

In java (or OOP), this is denoted as the “PART-OF” relationship between two classes. If one object is part-of another object e.g. Engine is part of Car, then association or relationship between them is Composition.

We can use final keyword to represent Composition in java programming. Since in Composition, Owner object expect part object to be available and functions, by making it final, your provide guarantee that, when Owner will be created, this part object will exist. This is actually a Java idiom to represent strong form of association i.e. composition between two objects.

Here, In the image shown, Objects of Class2 live and die with Class1. And, Class2 cannot stand by itself. The above two are forms of containment (hence the parent-child relationships).

Exemplary Java code :

public class Car {
    //final will make sure engine is initialized
    private final Engine engine;  
       
    public Car(){
       engine  = new Engine();
    }
}

class Engine {
    private String type;
}

Dependency: 
Dependency Exists between two classes if changes to the definition of one may cause changes to the other (but not the other way around). In other words, a using relationship between entities implying a change in specification of one entity may affect the entities that are dependent upon it.

More concretely, this translates to any type of reference to a class or object that doesn’t exist at the instance scope. This includes a local variable, reference to an object obtained via a method call, or reference to a class’s static method where an instance of that DEPclass does not exist. A dependency is also used to represent the relationship between packages. Because a package contains classes, you can illustrate that various packages have relationships between them based upon the relationships between the classes within those packages.

Dependency is a weaker form of relationship and in code terms indicates that a class uses another by parameter or return type.Dependency is also a form of association.

And here’s a complete example of an application.

Screenshot from 2014-02-21 06:12:43

References :

Advertisements