Class diagrams serve to illustrate a real-world representation of classes and their relationships.
The above diagram represents an overview of what a user would expect to see in a good class diagram. While the diagram contains quite a lot of information, it contains core features such as class attributes, generalizations, abstract classes, stereotypes, etc.
The above diagran consists of 2 classes and 1 interface. This is a good example of a class diagram, for it adheres to all of the rules used in creating class diagrams. In this class diagram, you see that the Person
class is a concrete class that contains a name
and age
attribute, as well as a getName
method. Since the Person
class implements the UserInterface
, it must implicitly define the getName
method, in which case, it does. The Employee
class extends from the Person
class. Therefore, it is considered to a subclass of the superclass Employee
If an instance of the Employee
class were to be created, then the employee object would have access to the name
and age
attributes, as well as the getName
method.
The diagram above is an example of a bad diagram, for the Person
class is an abstract class when it should be a concrete class. Since the Employee
class inherits from the Person
class, then both classes should have the ability to be instantiated. The other fault with this class diagram is that Employee
two of the same attributes as Person
, those of which are name
and age
. Since Employee
inherits from Person
, the Employee
object would have access to both the name
and age
attributes.