- https://mermaid.live/edit
- https://mermaid.js.org/syntax/classDiagram.html
- https://www.visual-paradigm.com/guide/uml-unified-modeling-language/uml-class-diagram-tutorial/
- [[Mermaid Class Diagram Examples]]
* [Design Patterns Tutorial](https://www.youtube.com/watch?v=vNHpsC5ng_E&list=PLF206E906175C7E07)
* [Design Patterns Tutorial 2](https://www.youtube.com/watch?v=yRJ1rRoMnIM&list=PLF206E906175C7E07&index=2)
* [[Inheritance and Polymorphism|Inheritance and Polymorphism]]
# Object Oriented Programming
* Organised in a way that reflects the real world
* All data and processes in one place: an object
# Design Principles
* Encapsulate what varies
* Favour Composition over Inheritance
* Program to Interfaces, not Implementation
```java
public class MyFrame extends JFrame implements ActionListener
is A has A
inheritance aggregation
```
## Overloading and Overriding
In the example below the constructor Sprite is overloaded. The method collision is overridden in the Monster child class.
```java
class Sprite {
Sprite(){
}
Sprite(int x, int y){
}
boolean collision(){
}
}
class Monster extends Sprite{
boolean collision(){
}
}
```
# Definitions
## Polymorphism
Methods inherited from a base class can be used in different ways depending on the data in the subclass that inherited it
or
A single interface is provided to entities/objects of different classes / types
## Encapsulation
The concept of putting properties, methods and data in one object
### Method
The code or routines contained within a class
### Properties
The defining features of an object or class in terms of its data
### Class
Defines the properties and methods of a group of similar objects
### Object
A specific instance of a class
## Public, Private and Protected
- Public means something can be accessed outside of the class it is in
- Private means it can only be accessed in the class it is in
- Protected means it can be accessed in the class it is in and in any subclasses
## Abstract, Virtual and Static Methods
### Static
The method used can be used without an object of the class being instantiated
### Virtual
- Virtual methods don't have to be overridden (but they can be) by the derived class
### Abstract
- Abstract methods must be overridden by the derived class;
## Inheritance (is a)
A subclass shares properties and methods with the class it is derived from
## Containment
An outer class contains an instance of another class and allows access to the contained object through its own methods.
## Aggregation (has a)
A method of creating new objects that contain existing objects
### Composition Aggregation (Solid Diamond)
Creating an object that contains other objects and will cease to exist if the containing object is destroyed. An example of this is a student object containing grades and attendance.
```mermaid
classDiagram
Student *-- Grade
Student *-- Attendance
class Student{
-Name : String
-Date of Birth: Date
+getName():String
}
class Grade{
-Grade:String
-Value: int
+ getGrade():String
}
class Attendance{
-Present: boolean
-DateAtt: Date
+isPresent(Date):boolean
}
```
Another good example of this is the Flys interface in the Strategy Design Pattern video here:
<https://www.youtube.com/watch?v=-NCgRD9-C6o&index=3&list=PLF206E906175C7E07>
(See UML diagram at 9:37)
### Association Aggregation (Outline Diamond)
Creating an object that contains other objects, which can continue to exist even in the containing object is destroyed
An example is a football team, made up of players, managers, coaches etc. If the football team ceases to exist, the players will just go to another team.
```mermaid
classDiagram
FootballTeam o-- Player
FootballTeam o-- Manager
class FootballTeam{
-Name : String
+getName():String
}
class Player{
-Name:String
+getName():String
}
class Manager{
-Name:String
+getName():String
}
```
## Exercise
An object-oriented program is required to handle details of a lending library’s books and DVDs.
Some fields required for the books are:
* Title
* Author
* ISBN
* DateLoaned
* DateReturned
Some fields required for the DVDs are:
* Title
* Running Length
* DateLoaned
* DateReturned
Some methods required are:
* SetLoan
* DisplayDetails
This could be implemented by declaring two separate classes Book and DVD. This would result in a lot of repetitive code.
1. Making use of inheritance, write class definitions for one superclass StockItem and two subclasses Book and DVD.
2. Draw a class diagram of the above
A class Library is created. This will contain a list of all the books and DVDs in the library.
1. What OOP principle is used here?
2. Update your class diagram with the Library class