CSCI 1301

Class Diagram

1. Learning Objectives

  • What is a Class Diagram
  • How to create a simple class diagram
  • How to convert class diagram to code

2. What is Class Diagram

Unified Modeling Language (UML) is a collection of various diagrams used to represent programming concepts visually. UML allows us to visualize and communicate program structures in a graphical and concise way.

UML contains definitions for multiple different types of diagrams. A class diagram is one type of diagram defined in by UML. Class diagrams (as the name implies) are used for representing classes.

Below is a very simple version of a class diagram. While very simple, it still contains a lot of interesting information. Spend a moment viewing it and try to guess what it means.

class diagram

Class diagram represents each class by its name, properties and methods. The diagram has three corresponding sections:

  • top section contains the class name
  • middle section lists the class properties
  • bottom section describes the class methods

More specifically, a class diagram allows communicating the following information:

  • What properties should exist
  • What methods should exist
  • Access modifiers for properties and methods
  • Parameters for each method
  • Return types for each method

In a more advanced scenarios class diagrams are also used to represent relationships between classes, but we will not go that far. Just be aware if you look up more examples independently, you will see these more advances variations of a class diagrams.

3. Reading the Diagram

When you read a class diagram note the following:

  • the name of the class is top center
  • - means something is private (it is not a bullet list!)
  • + means something is public - these are the access modifiers
  • each class property is listed with its data type (middle section)
  • each class method is listed with its parameters and return type

While the diagram does not provide details about exact behavior of methods, it provides the necessary of information for implementing everything else.

4. Using to Create Class Diagrams

You can use almost any drawing tool to create class diagrams, but I recommend because it comes with some built-in UML tools and it is free to use. Here is how to create a simple class diagram with (detailed instructions will follow):

step 1

1. Create diagram

step 1

2. Choose blank diagram, then "create"

step 2

3. Search for "class" and you will find a ready to use diagram

step 3

4. Now edit your class diagram

step 4

5. Save Your Diagram

5. Converting Class Diagram to Code

Assume we are looking at this same diagram, but now we want to implement code based on this diagram.

class diagram

To convert the diagram to code:

  1. Create a class with the specified name
  2. Add properties matching the diagram
  3. Add methods matching the diagram

Below is an implementation of the same class in C#. Spend a few minutes to understand how the two correspond.

1public class Car // ← from top row of class diagram
3 private String color; // ← from middle section
4 private String year; // ← of class diagram
5 private int mileage; // ←
7 // from bottom section of class diagram ↓↓↓
9 public String GetColor(){
10 return color;
11 }
13 public void SetColor(String color){
14 this.color = color;
15 }
17 public String GetYear(){
18 return year;
19 }
21 public void SetYear(String year){
22 this.year = year;
23 }
25 public void Drive(int miles)
26 {
27 // implementation here
28 }
30 public override string ToString()
31 {
32 return $"Color: {color}\n" +
33 $"year: {year}\n" +
34 $"mileage: {mileage}";
35 }

6. Summary

In this module you learned about class diagrams

What is a Class Diagram

Class diagram is a visual representation of a class and allows to communicate the name, properties, methods, method parameters, and method return types of a class in a concise way

How to create a simple class diagram

Create a rectangle with 3 sections (split horizontally):

  1. Top section as class name
  2. Middle section lists class properties
  3. Bottom section lists class methods

How to convert class diagram to code

After learning how to read a class diagram, you can take the provided information and convert that to a class with the specified properties and methods.