UML Class Diagram Examples for Java: OOP Patterns Visualized
Table of Contents
Java is one of the most common languages where class diagrams come up — in university courses, design discussions, and code reviews. Whether you are taking an OOP class, preparing for a system design interview, or documenting a Java service, this post has the diagrams you need.
Each example below uses Mermaid class diagram syntax so you can paste it directly into the free Badger tool and export a PNG or SVG immediately.
Inheritance: Classic Animal Hierarchy
The most common first example in every Java OOP course:
classDiagram
class Animal {
+String name
+int age
+makeSound() void
+move() void
}
class Dog {
+String breed
+fetch() void
}
class Cat {
+bool isIndoor
+purr() void
}
class GoldenRetriever {
+String registrationNumber
}
Animal <|-- Dog
Animal <|-- Cat
Dog <|-- GoldenRetriever
This shows a three-level hierarchy: Animal at the top, Dog and Cat as subclasses, GoldenRetriever as a further specialization of Dog. The <|-- arrows represent the extends keyword in Java.
Interface and Implementation (Realization)
Java interfaces map to the realization relationship in UML. The dashed line with hollow triangle represents the implements keyword:
classDiagram
class Printable {
<<interface>>
+print() void
}
class Saveable {
<<interface>>
+save(path String) bool
}
class Document {
+String content
+String filename
+print() void
+save(path String) bool
}
Printable ..|> Document
Saveable ..|> Document
Document implements both Printable and Saveable. In Java: class Document implements Printable, Saveable. The ..|> relationship shows each interface implementation.
Sell Custom Apparel — We Handle Printing & Free ShippingComposition: Order and OrderItem
A common e-commerce domain model. An Order owns its OrderItems — they do not exist outside the Order:
classDiagram
class Order {
+int orderId
+Date createdAt
+float totalAmount
+addItem(item OrderItem) void
+calculateTotal() float
+cancel() bool
}
class OrderItem {
+int quantity
+float unitPrice
+String productSku
+getLineTotal() float
}
class Customer {
+String customerId
+String email
+String name
}
Order *-- OrderItem
Customer "1" --> "0..*" Order : places
The filled diamond (*--) between Order and OrderItem shows composition — delete the Order and the OrderItems are gone too. The association from Customer to Order shows the relationship without ownership.
Abstract Class Pattern in Java
Abstract classes in Java sit between concrete classes and interfaces — they can have implemented methods and abstract ones. Mark them with the <<abstract>> stereotype in Mermaid:
classDiagram
class Shape {
<<abstract>>
+String color
+calculateArea() float
+calculatePerimeter() float
+draw() void
}
class Circle {
+float radius
+calculateArea() float
+calculatePerimeter() float
}
class Rectangle {
+float width
+float height
+calculateArea() float
+calculatePerimeter() float
}
Shape <|-- Circle
Shape <|-- Rectangle
Shape is abstract — it defines the contract but does not implement calculateArea() or calculatePerimeter(). Circle and Rectangle provide the implementations.
Service and Repository Pattern (Spring-Style)
A typical pattern in Spring Java applications — service layer depending on repository interfaces:
classDiagram
class UserRepository {
<<interface>>
+findById(id Long) User
+findByEmail(email String) User
+save(user User) User
+delete(id Long) void
}
class UserService {
-UserRepository userRepo
+getUserById(id Long) User
+createUser(data UserDto) User
+updateEmail(id Long, email String) void
+deleteUser(id Long) void
}
class User {
+Long id
+String email
+String passwordHash
+Date createdAt
}
UserService ..> UserRepository : uses
UserService ..> User
UserRepository ..> User
UserService depends on UserRepository (dashed arrow, dependency relationship) — it calls the repository in its methods but does not own it. This reflects Spring's dependency injection pattern.
Try It Free — No Signup Required
Runs 100% in your browser. No data is collected, stored, or sent anywhere.
Open Free Class Diagram ToolFrequently Asked Questions
How do I show the extends keyword in a Java class diagram?
Use the inheritance arrow: Animal <|-- Dog means Dog extends Animal. The hollow triangle points at the parent class.
How do I show the implements keyword in a Java class diagram?
Use the realization arrow: Printable ..|> Document means Document implements Printable. The dashed line with hollow triangle points at the interface.
How do I mark an abstract class in Mermaid?
Add <
Can I auto-generate a class diagram from Java code?
Some IDE plugins (like those for IntelliJ or Eclipse) can generate UML from Java source. The Badger tool requires you to write Mermaid syntax manually — which gives you control over what to include and exclude.

