Exam preparation
Overview of interesting stuff from the lecture slides (that I sometimes forget)
Enumeration types
Can have methods and attributes, but only a limited amount of constant instances.
```[Java] enum Direction { N, E, S, W; public Direction turnleft(){ switch (this) { case N: return Direction.W; case E: return Direction.N; case S: return Direction.E; case W: return Direction.S; } } }
Direction dir = Direction.N; dir = dir.turnLeft(); ```
Comparing reference types
Given two objects o1 and o2, o1==o2 does not compare value but pointers: only returns true if the object is the exact same object.
If you want to compare on values, define your own equals method.
Static methods
Static methods / services do not have an implicit this argument passed as a parameter. This means that they cannot manipulate normal attributes or call other non-static methods.
A class can only have one static attribute, which are shared by all class instances. This attribute can be accessed by a static method.
Setters
Use a single setter for attributes that are mutually dependent, to avoid incorrect combinations of values.
Subtype rule:
If a type B is a subtype of type A, then a B value can be provided wherever an A value is required. But not the other way around.
Let's say we have a supertype 'Vehicle' and a subtype 'Car'. Then a function that is defined on all instances of type 'Vehicle', but is called on a 'Car' instance, dynamically binds to the 'Car' instance.