• Tuesday, 16-Sep-25 19:07:40 IST
Tech Trending :
* Semantic Search Using Text Embeddings (With ChatGPT + Python) * πŸ€–How OpenAI + MCP Servers Can Power the Next Generation of AI Agents for Automation * πŸ“š Book Recommendation System Using OpenAI Embeddings And Nomic Atlas Visualization

πŸ”Ή Understanding UML Relationships in Java with Examples

πŸ”Ή Understanding UML Relationships in Java with Examples

When designing object-oriented systems, relationships between classes form the backbone of your architecture. UML (Unified Modeling Language) provides different ways to represent these relationships. In Java, we can demonstrate these relationships with code examples.

In this blog, we’ll explore:

  • Association

  • Aggregation

  • Composition

  • Inheritance

  • Dependency

  • Realization

Each concept is explained with Java code and represented in a UML diagram.


1. Association – Objects are related but independent

Association represents a β€œuses” relationship where two classes can interact but still exist independently.

Example:

public class AssociationExample { public static void main(String[] args) { Teacher teacher = new Teacher("Miss Neha"); Student student = new Student("Rahul"); teacher.teach(student); } } class Teacher { private String name; public Teacher(String name) { this.name = name; } public String getName() { return name; } public void teach(Student student) { System.out.println(name + " is teaching " + student.getName()); } } class Student { private String name; public Student(String name) { this.name = name; } public String getName() { return name; } }

Output:

Miss Neha is teaching Rahul

2. Aggregation – Weak "has-a" relationship

Aggregation is a whole-part relationship, but the part can exist independently of the whole.

Example:

public class AggregationExample { public static void main(String[] args) { Professor prof1 = new Professor("Dr John"); Professor prof2 = new Professor("Dr Ashish"); List<Professor> professorList = Arrays.asList(prof1, prof2); Department department = new Department("Computer Science", professorList); department.showProfessor(); } } class Professor { private String name; public Professor(String name) { this.name = name; } public String getName() { return name; } } class Department { private String name; private List<Professor> professorList; public Department(String name, List<Professor> professorList) { this.name = name; this.professorList = professorList; } public void showProfessor() { System.out.println("Department: " + name); professorList.forEach(prof -> System.out.println(prof.getName())); } }

Output:

Department: Computer Science Dr John Dr Ashish

3. Composition – Strong "has-a" relationship

Composition is also a whole-part relationship, but the part cannot exist without the whole.

Example:

public class CompositionExample { public static void main(String[] args) { House house = new House(); house.showRooms(); } } class Room { private String name; public Room(String name) { this.name = name; } public String getName() { return name; } } class House { private List<Room> rooms; public House() { rooms = new ArrayList<>(); rooms.add(new Room("Living Room")); rooms.add(new Room("Bedroom")); } public void showRooms() { rooms.forEach(room -> System.out.println(room.getName())); } }

Output:

Living Room Bedroom

If the House object is destroyed, its Room objects are also destroyed β†’ strong ownership.


4. Inheritance – β€œis-a” relationship

Inheritance means a subclass inherits the properties and methods of a superclass.

Example:

public class InheritanceExample { public static void main(String[] args) { Dog dog = new Dog(); dog.bark(); dog.eat(); } } class Animal { public void eat() { System.out.println("This animal is eating"); } } class Dog extends Animal { public void bark() { System.out.println("The dog is barking"); } }

Output:

The dog is barking This animal is eating

5. Dependency – One class depends on another

Dependency occurs when one class uses another temporarily for its functionality.

Example:

public class DependencyExample { public static void main(String[] args) { Document document = new Document("Dependency Example Document"); Printer printer = new Printer(); printer.print(document); } } class Document { private String content; public Document(String content) { this.content = content; } public String getContent() { return content; } } class Printer { public void print(Document document) { System.out.println("Printing document: " + document.getContent()); } }

Output:

Printing document: Dependency Example Document

6. Realization – Implementing an Interface

Realization is when a class implements the behavior defined in an interface.

Example:

interface AnimalActions { void makeSound(); } class Cat implements AnimalActions { @Override public void makeSound() { System.out.println("Meow"); } } public class RealizationExample { public static void main(String[] args) { AnimalActions cat = new Cat(); cat.makeSound(); } }

Output:

Meow

UML Diagram of All Relationships

Here’s a UML diagram that summarizes these six relationships:

  • Association β†’ Teacher ↔ Student

  • Aggregation β†’ Department β—‡ Professor

  • Composition β†’ House β—† Room

  • Inheritance β†’ Dog β†’ Animal

  • Dependency β†’ Printer β†’ Document

  • Realization β†’ Cat β‡’ AnimalActions


  • Conclusion

    We explored six fundamental UML/OOP relationships with Java examples:

    • Association β†’ Objects are related but independent.

    • Aggregation β†’ Weak β€œhas-a” relationship, parts can exist independently.

    • Composition β†’ Strong β€œhas-a” relationship, parts die with the whole.

    • Inheritance β†’ β€œIs-a” relationship between superclass and subclass.

    • Dependency β†’ One class temporarily depends on another.

    • Realization β†’ Class implements interface behavior.

    These relationships are the building blocks for designing clean, extensible, and maintainable software systems.