UML class and sequence diagram example in Java
UML is used in modelling and design phase of an application. The purpose of these language is to write better and efficient code. UML helps in visualizing the system before the coding process starts. By doing this, a lot of bugs can be avoided at later stage which results in saving the maintenance cost.
In a Java interview, if you are asked about UML and have worked on it in your earlier projects then do speak about your experience in using UML. The most common type of diagrams are use case diagrams, class diagrams and sequence diagrams. While use case diagrams are used to depict a particular business scenario, the class diagrams are used to represent the structure of a class and sequence diagrams are used show the relationship between various classes and more importantly the logical flow between them.
As mentioned above, the class diagram used to show the member variables and methods of a class. Additionally, the class diagram shows the relationship between various classes. These relationships include inheritance, composition, association and aggregation etc.
This diagram contains static data about a class which includes the access modifier, constructors and type (interface, abstract class or concrete class).
A class diagram looks like:
A sequence diagram is a dynamic message sequence chart which shows the interaction between various classes and the messages being exchanged with respect to time. While the classes are arranged vertically, the messages are shown as horizontal arrows. Sometimes the messages are numbered to help identify the sequence in which they are invoked.
In addition to the classes of application, the users are also shown in the sequence diagram. These diagrams are very effective way of showing how a particular sub-system behaves.
A sequence diagram looks like: