Class diagrams are the main building block in object-oriented modeling. They are used to show the different objects in a system, their attributes, their operations and the relationships among them.
Classes are interrelated to each other in specific ways. In particular, relationships in class diagrams include different types of logical connections. The following are such types of logical connections that are possible in Unified Modelling Language:
An example to understand the relationships
consider a person with his/her gadgets (phones, Laptops etc )
From here we have 2 classes class person and class gadget
when a person charges a gadget that creates an Association between the two classes
Composition implies a relationship where the child cannot exist independent of the parent.
Battery is part of a phone
A phone can't exist without a battery hence that is an example of composition
A battery and a phone can however exist independently . This is an example of Aggregation
Aggregation implies a relationship where the child can exist independently of the parent
Generalization is a mechanism for combining similar classes of objects into a single, more general class.
Phones and Laptops are examples of Gadgets .
class phones can be a subclass of class Gadgets
This is an example of Generalization as phones inherit attributes from the super class Gadgets
Specialization is the reverse process of Generalization means creating new sub-classes from an existing class hence in the example we could have class phones being created from class Gadgets
Representation of the relationships