UML stands for “Unified Modeling Language”.
The term modeling is “to create a simplified representation of something.” So, UML is a graphical notation for modeling computer programs.
Why Do We Need UML?
The reason is, when we want to understand a computer program, what a program is supposed to do, how different parts of the program are working in relation- this time we go towards UML. We do not need to go towards the detailed analysis of the code, because the trouble with the code is that it is very detailed. But UML gives us a bigger picture of major parts of a program and how they work together. UML uses different kinds of diagrams(Class diagrams, object diagrams, sequence diagrams, use case diagrams, and so on) that provide different ways to look at a program and its operations.
Relations Between Classes:
Different Types Of Relations:
1: Inheritance
2: Association
1: Inheritance:
Inheritance is the process of creating new classes, called derived classes, from existing classes called base classes. The derived class inherits all the properties of the base class but can have some of its own. Inheritance is “IS A ” relationship. In which both classes are tightly coupled.
Explanation:
Derived class “Car” is derived from base class “Vehicle”. Here “Is A” relationship exists. Car “Is A” vehicle. The vehicle has characteristics shared by all cars(drive, engine, seats, tyres) but has some distinctive characteristics of its own(red color, specific sound). For this reason, inheritance is also called a “kind of” relationship.
In UML class diagrams, inheritance is indicated by a triangular head on the line connecting derived and base classes. The derived class is at the tail of the arrowhead and the base class is on top.
2: Association:
Association is a “HAS A” relationship. It implies that objects of different classes have some kind of relationship. In the UML class diagram, the association is indicated with a line connecting both classes. It can be unidirectional or bidirectional. An association relationship can be represented as one-to-one, one-to-many, or many-to-many.
Suppose we have two classes, doctors and patients. Doctor class has objects d1,d2,d3. Patient class has objects p1,p2,p3. P1 can visit many doctors and one doctor can check many patients. There is no dependency of both objects on each other. Both have individual life cycles. If we destroy one object, others will not be affected and vice versa. In short, both classes are not tightly coupled.
Two Types Of Association:
Aggregation:
Aggregation is a type of association. It is a “Whole/Part” relationship. The book is part of the library. Here we use the concept of ownership. One class is considered the owner(Whole) and another class(part) are owned by that owner. The lifetime of a part is not the same as the lifetime of the whole. In the UML class diagram, aggregation is shown in a way that, the “whole” end of the association line has an open diamond-shaped arrowhead.
Suppose we have a classes Library, Books and Staff. Here Library is the owner that owned Books and Staff. Books and Staff classes have aggregation relationship with the Library class. In aggregation, part is not dependent upon the whole. So, objects of Book class and Staff class are not lifetimes dependent on Library class. If we destroy Library’s objects, it will never affect the Staff’s objects. Staff’s objects can exist independently.
Composition:
Composition(restricted aggregation) is also called the death relationship. Composition is a “consist of ” relationship. Here, we also use the concept of Whole/Part. It has all characteristics of aggregation, plus two more.
1: The part may belong to only one whole. 2: The lifetime of the part is the same as the lifetime of the whole.
In the UML class diagram, composition representation is the same as aggregation except that diamond is filled instead of empty.
Suppose, we have classes in Room, Walls, and Floor. Parts(Walls, Floor) are related to the Whole(Room) by composition. If we remove the whole(Room), the part(walls) will not exist.
Conclusion:
UML is a way of representing ideas, working, and associations of different classes and objects. Relations are used either to outsource some work or to inherit some characteristics. This is all about UML and Relations.
See you next time. Till then, keep learning and keep exploring :)