UML relations notation
IS-A (Inheritance) : extends / implements(realization)
class Laptop { // Code for Laptop class goes here. } interface Formatable { // Members of Interface. } class Dell extends Laptop implements Formatable { // More code related to Dell goes here. // Dell class will inherit all accessible members of Laptop class. // Dell IS-A Laptop. // And Dells class also implements all method of Formatable interface, since // Dell is not an abstract class. // so Dell IS-A Formatable. }
Has-A (Association) :
class HardDisk { public void writeData(String data) { System.out.println("Data is being written : " + data); } } class UseDell { // segate is referece of HardDisk class in UseDell class. // So, UseDell Has-A HardDisk HardDisk segate = new HardDisk(); public void save (String data) { segate.writeData(data); } }
Aggregation :
Consider above example : HardDisk class and UseDell class.
In the example, UseDell HAS-A HardDisk.
If Laptop stops working somehow, you can remove harddisk from that and connect to other laptop, This is Aggregation.
Composition :
class House {
Kitchen kitchen = new Kitchen();
// More code for House class.
}
class Kitchen {
// code of Kitchen class.
}
If House gets destroyed Kitchen also will be destroyed with that House, This is composition.
In composition reference class (Kitchen) can not exist if container class (House) gets destroyed.
In the example, UseDell HAS-A HardDisk.
In composition reference class (Kitchen) can not exist if container class (House) gets destroyed.