Home UML Basics Class Diagram Use Case Diagram Statechart Diagram Activity Diagram Sequence Diagram Communication Diagram Deployment Diagram Need Help?
Class Diagram

The Basics

Remember that UML is used generically to apply to any object-oriented language, and recall that object-oriented refers to classes, which are used to be containers of information for what we call objects. Class diagrams are supposed to be generic examples of a collection of classes in a system in UML. Most importantly, class diagrams can show you the specific associations between classes like inheritance and how classes are expected to give each other information. This is an important step for modeling software because it starts to outline key features that will be needed when actually programming.

How to Choose a Class

While that brief introduction to class diagrams is all well and good, choosing classes that will be necessary and useful for your actual class diagram is what you want to know. Well, firstly, you need to develop your use cases! If you're unfamiliar with use cases, see our section on use cases for an overview, but briefly, use cases are simply the ways a user will use your system. A simple rule to remember for classes: find the nouns and verbs. In any scenario in software development, you're going to have user scenarios that have nouns and verbs. The nouns will represent classes as they can be turned into objects (hence noun), and the verbs are the methods of your class because they are doing something. Think about your search function in your list class; it searches your list when called. Now, you just have to apply that to making a class. Note: the nouns and verbs rule is general, but sometimes your noun should be an attribute of your class. A common example used for introducing class diagramming is the pizza shop example.

The Pizza Order Example

Photo by Jorge Fakhouri Filho from Pexels

A pizza restaurant chain wants to set up an online ordering system. They want the customer to be able to order a pizza online with their payment information and address. The customer should also be able to choose their pizza toppings and size. This order should be sent back to the specific restaurant the customer ordered from, and the customer should receive the cost of their order.

You should be able to identify several nouns: pizza chain, order, customer, pizza, address, payment, toppings, size, and cost. There should also be a few verbs chosen: choose, ordered, receive This should be a good basis for a class diagram. Try it out for yourself before looking at our diagram below! The "+" is to show public attributes and "-" stands for private attributes. There's another symbol "#" that stands for protected members, but that is used less often. Again, note that the class system above is not necessarily a "correct" class diagram. There are a lot of ways to design a system, but we're just trying to get you to think about designing a system within the context of a Class Diagram. This is just a simple interpretation of this particular system. The top of our "class box" should have the name of the class in it.


The Pizza Order Example Class Diagram

We didn't use receive based on our simple interpretation. An important thing to learn about using the nouns and verbs method is that NOT all verbs or nouns will actually be useful to us. If we added a cashier class in that printed a receipt for a driver to bring to the customer, receive could become more relevant.