UML: Class Diagram

UML: Class Diagram

Class

1. Normal Class

class_diagram

2. Abstract Class

There are two ways to show an abstract class, use a italicized class name or use {abstract} property:

class_diagram_2

An abstract can have attributes and abstract methods, and it cannot have implemented methods. We can use abstract classs to achieve code reusability:

class_diagram_3

3. Interface

An interface can only have abstract methods, and it cannot have attributes and implemented methods. We can use interface to achieve loose coupling:

class_diagram_4

Relations

class_diagram_5

  1. Generalization: A class can generalize(or inherit from) a superclass or an abstract class.

  2. Realization(Implementation): A class can implement an interface.

  3. Dependency and association:

1
2
3
4
5
6
7
8
public class A {
private C c;// A has-a C object as a member variable, this is called association.

// A references B (as a method parameter or return type), this is called dependency.
public void foo(B b) {
b.callMethod();
}
}

Note that association can be directed or undirected(in practice we normally use undirected association, namely we do not use an arrow):

class_diagram_6

  1. Aggregation and Composition:

class_diagram_7