Relationships between Classes
- Association
- Composition
- Aggregation
- Generalization
- Realization
- Dependency
Association(Has-A Relationship)
- Association is a relationship between two separate classes that establish through their objects. Each objects have their own life-cycle and there is no owner. Association can be one-to-one, one-to-many, many-to-one, many-to-many. It isn’t a “has-a” relationship and only means that the objects “know” each other
class Bank { private String name; Bank(String name){ this.name = name; } public String getBankName(){ return this.name; } } class Employee { private String name; Employee(String name){ this.name = name; } public String getEmployeeName(){ return this.name; } } class Association { public static void main (String[] args) { Bank bank = new Bank("Axis"); Employee emp = new Employee("Mani"); System.out.println(emp.getEmployeeName() + " is employee of " + bank.getBankName()); } }
In above example two separate classes Bank and Employee are associated through their Objects. Bank can have many employees, So it is a one-to-many relationship.
- Aggregation and Composition are subsets of association meaning they are specific cases of association.Both of them conveys a has-a relationship to strong and weak degree
- Composition(Strong has-a relationship)
- Aggregation(Weak has-a relationship)
- has-a relationship simply means that an instance of one class has a reference to an instance of another class or an other instance of the same class
- Composition denotes a has-a relationship of Strong degree. Relationship among class members are strong.Classes which are grouped by Composition doesnot makes sense when interpreted seperately. I.E. when Person, Heart, Hand were seperated Heart and Hand doesnot make much sense.Objects in a composition relationship cannot, conceptually, exist in isolation. If we destroy the owner object, its members also will be destroyed with it
public class WebPage { private final PageHeader header; private final PageBody pageBody; public WebPage(PageHeader header, PageBody pageBody) { this.header = header; this.pageBody = pageBody; } } class PageHeader { private String title; private String charset; } class PageBody { private String body; }
- Aggregation denotes a has-a relationship of Weak degree.Relationship among class members are weak. I.E. When trees were seperated from City Class Trees still make perfect sense and stand on it own.Objects on both sides of an aggregation relationship can exist in isolation.
public class Team { private List<Player> players = new ArrayList<>(); public void addPlayer(Player player) { this.players.add(player); player.setTeam(this); } public void removePlayer(Player player) { this.players.remove(player); player.setTeam(null); } } class Player { private String name; private int age; public void setTeam(Team team) { this.team = team; } }
- In UML notation, a composition is denoted by a filled diamond, while aggregation is denoted by an empty diamond, which shows their obvious difference in terms of strength of the relationship.
Composition vs Aggregation
Generalization(Is-A Relationship)
- In inheritance, a child of any parent can access, update, or inherit the functionality as specified inside the parent object. A child object can add its functionality to itself as well as inherit the structure and behavior of a parent object.This type of relationship collectively known as a generalization relationship.
-
public class Vehicle { } class Truck extends Vehicle { } class Boat extends Vehicle { }
Realization(interfaces)
- In a realization relationship of UML, one entity denotes some responsibility which is not implemented by itself and the other entity that implements them. This relationship is mostly found in the case of interfaces.
- Realization is a specialized abstraction relationship between two sets of model elements, one representing a specification (the supplier) and the other represents an implementation of the latter (the client).
-
class Hyndai implements Car { . . }
Dependency
- In a dependency relationship, as the name suggests, two or more elements are dependent on each other.Dependency relationships exist when classes depend on each other in such a way that a change to one class may affect the other, such as when one class accepts an instance of another class as parameter to a method.
-
Dependency –> A references B (as a method parameter or return type)
public class A { private C c; public void myMethod(B b) { b.callMethod(); } }
- Dependency relationships are represented by arrows on dashed lines. Dependency represents the weakest of relationship
- A dependency relationship can exist when we have a Library class that manages Book objects. Since the Library class has a method that returns a Book, changes to the Book class could result in changes to the Library class (based on how Book objects are created).
public class Library { public Book findBook(String name) { //Do some book stuff. return new Book(); } } class Book { private String name; private String author; }
- A dependency typically (but not always) implies that an object accepts another object as a method parameter, instantiates, or uses another object. A dependency is very much implied by an association.