Class Diagrams

ICT2622 - Object-Oriented Analysis · Object-Oriented Modelling Techniques

Class Diagrams

Class diagrams are a fundamental part of object-oriented modelling in software development. They provide a visual representation of the classes in a system and their relationships. Class diagrams are part of the Unified Modeling Language (UML), which is a standard way to visualise the design of a system.

Understanding Classes

A class is a blueprint for creating objects. It defines the properties (attributes) and behaviours (methods) that the objects created from the class will have. For example, consider a class called Car.

class Car {
String make;
String model;
int year;
void start() {
// code to start the car
}
}

In this example, the Car class has three attributes: make, model, and year. It also has a method called start that defines an action that can be performed by a car object.

Class Diagram Components

A class diagram consists of the following components:

  • Classes: Represented by rectangles divided into three sections: the class name, attributes, and methods.
  • Attributes: Listed in the second section of the class rectangle, showing the data that the class holds.
  • Methods: Listed in the third section, showing the functions that can be performed on the class.
  • Relationships: Lines that connect classes, indicating how they interact with each other.

Creating a Class Diagram

To create a class diagram, follow these steps:

  1. Identify the classes needed for the system.
  2. Define the attributes and methods for each class.
  3. Determine the relationships between the classes.
  4. Draw the diagram using UML notation.

Example of a Class Diagram

Let’s create a class diagram for a simple library system. We will have three classes: Book, Member, and Loan.

Step 1: Identify Classes

The classes are:

  • Book
  • Member
  • Loan

Step 2: Define Attributes and Methods

For each class, we will define attributes and methods:

  • Book
    • Attributes: title, author, ISBN
    • Methods: checkAvailability(), reserve()
  • Member
    • Attributes: memberID, name, email
    • Methods: borrowBook(), returnBook()
  • Loan
    • Attributes: loanID, loanDate, returnDate
    • Methods: createLoan(), closeLoan()

Step 3: Determine Relationships

Next, we need to define the relationships:

  • A Member can borrow many Books.
  • A Book can be associated with many Loans.

Step 4: Draw the Diagram

The class diagram can be represented as follows:

+-----------------------+    +-----------------------+
| Book | | Member |
+-----------------------+ +-----------------------+
| - title: String | | - memberID: String |
| - author: String | | - name: String |
| - ISBN: String | | - email: String |
+-----------------------+ +-----------------------+
| + checkAvailability() | | + borrowBook() |
| + reserve() | | + returnBook() |
+-----------------------+ +-----------------------+
| |
| |
| |
| |
| |
+-----------------------+
| Loan |
+-----------------------+
| - loanID: String |
| - loanDate: Date |
| - returnDate: Date |
+-----------------------+
| + createLoan() |
| + closeLoan() |
+-----------------------+

Remember: In UML, a solid line indicates a direct relationship, while a dashed line indicates a dependency.

Types of Relationships

There are several types of relationships in class diagrams:

  • Association: A basic relationship where one class uses another. For example, a Member borrows a Book.
  • Aggregation: A special type of association that represents a whole-part relationship. For example, a Library has Books.
  • Composition: A stronger form of aggregation where the part cannot exist without the whole. For example, a Car has an Engine.
  • Inheritance: A relationship where one class (the subclass) inherits attributes and methods from another class (the superclass). For example, a SportsCar class that inherits from the Car class.

Example of Inheritance

Consider the following classes:

class Vehicle {
String type;
}

class Car extends Vehicle {
int numberOfDoors;
}

class Truck extends Vehicle {
int loadCapacity;
}

Here, Car and Truck are subclasses of Vehicle. They inherit the type attribute from Vehicle.

Watch out: Do not confuse aggregation with association. In aggregation, the life cycle of the part is independent of the whole.

Best Practices for Class Diagrams

When creating class diagrams, consider the following best practices:

  • Keep it simple: Only include necessary classes and relationships.
  • Use clear naming conventions: Class names should be nouns, and method names should be verbs.
  • Be consistent: Use the same notation throughout the diagram.
  • Review regularly: Update the diagram as the system evolves.

Summary

  • A class diagram represents the structure of a system using classes and their relationships.
  • Classes contain attributes and methods.
  • Relationships include association, aggregation, composition, and inheritance.
  • Follow best practices for clarity and consistency.

Check your understanding

  1. What is a class in object-oriented programming?
  2. Explain the difference between aggregation and association.
  3. What is the purpose of a class diagram?
  4. How do you represent a method in a class diagram?