Created: 2023-08-28 10:04
Status: #concept
Subject: Programming
Tags: Java Java Class UML
Class Diagram
It is a programmatically generated visual notation to represent Java Class structures.
- it describes
class
attributes, constructors, and method contracts, and the connections between other classes, but not the underlying implementation.
Syntax
- a
+
means thepublic
keyword is used, otherwise-
denotesprivate
. - arrows
->
represent semantic relationships, for exampleBook -> Person
means aBook
class keeps a reference of thePerson
who wrote the book, butPerson
doesn't. - an arrow with an asterisk
->*
indicates a One-to-many relationship, so the origin class has an ArrayList of the class it points to. - if there is no arrow
-
, then both classes keep track of a reference of each other.
Describing Inheritance
If a subclass
extends
a superclass, we represent that relationship with a hollow triangle arrowhead child -|> parent
.
- if we inherit a Java Abstract Class, we add the
<<abstract>>
italic label on top of the class name and make all itsabstract
methods italic. - similarly, when implementing Java Interfaces, we use the same notation but with
<<interface>>
on top of the class name and a dashed arrow- -|>
.
Function Signature Syntax
Some examples of common functions in a
Person
class:
+Person(name: String)
- the Java Class constructor.-name: String
aprivate
attribute.+sayHi(): void
- apublic
Method that takes in noting & returns nothing.+getName(): String
- a getter method that returns thethis.name
instance variable.+setName(String)
- a setter method to changethis.name
of the instance.