UML Diagrams

UML Class Diagrams

Visual representation of classes, their attributes, methods, and relationships. Essential for LLD interviews.

15 min readEssential for Interviews

1Class Notation

A class is represented as a rectangle divided into three sections: class name (top), attributes (middle), and methods (bottom).

Class Box Structure

ClassName- privateAttr: Type+ publicAttr: Type# protectedAttr: Type- privateMethod(): void+ publicMethod(): Type# protectedMethod(): void~ packageMethod(): void
+
Public
Accessible anywhere
-
Private
Class only
#
Protected
Class + subclasses
~
Package
Same package

2Relationship Notation

UML Relationship Symbols

AssociationUses or referencesAggregationHas-a (weak), empty diamondCompositionHas-a (strong), filled diamondInheritanceIs-a, extends, empty triangleRealizationImplements interface, dashedDependencyUses temporarily, dashed arrow
Memory Aid for Diamonds
  • Filled Diamond (Composition): "Filled = Full ownership" - part dies with whole
  • Empty Diamond (Aggregation): "Empty = External" - part can exist independently

3Multiplicity

NotationMeaningExample
1Exactly oneOrder has 1 Customer
0..1Zero or one (optional)Person has 0..1 Spouse
*Zero or moreCustomer has * Orders
1..*One or moreOrder has 1..* Items
n..mSpecific rangeCar has 3..4 Wheels

4Complete Example: E-Commerce

E-Commerce Class Diagram

Customer- id: String+ placeOrder(): Order+ getOrders(): ListOrder- orderId: String- status: OrderStatus+ addItem(): void+ calculateTotal(): doubleOrderItem- quantity: int+ getSubtotal(): doubleProduct- price: double+ getPrice(): doublePayment+ process(): booleanabstractplaces1*1..*1
Customer places Orders (1 to many). Orders contain OrderItems (composition). OrderItems reference Products.

5Interview Follow-up Questions

Interview Follow-up Questions

Common follow-up questions interviewers ask

6Key Takeaways

1Class box has three sections: name, attributes, methods.
2Visibility: + public, - private, # protected.
3Filled diamond = Composition (strong), Empty diamond = Aggregation (weak).
4Hollow triangle = Inheritance/Realization, Arrow = Association/Dependency.
5Multiplicity shows how many: 1, *, 1..*, 0..1.
6Practice drawing diagrams for common problems (Parking Lot, etc.).