You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+74-12
Original file line number
Diff line number
Diff line change
@@ -5,22 +5,34 @@
5
5
* Software development paradigm
6
6
* We should represent our system concepts using classes
7
7
* Classes deals with common behaviour to all of its different instances (objects):
8
+
8
9

9
10
* Objects have their own memory
10
11
* Object communicate between them sending and receiving messages
11
12
12
13
## Visibility and inheritance
13
14
14
-
### `public`, `protected`, and `private`
15
+
### `public`, `protected`, and `private` visibility keywords
15
16
* When do you use each one?
17
+
* Guilt presumption
18
+
* Simplify our classes API (exposed methods) => Easier to understand, easier to be SRP compliant, avoid having to maintain public methods because others are coupled to them
16
19
* Question:
17
20
* Which would be the output of the `Child#visibilityTest` method?
18
-
* Solution: `ChildShould`
21
+
* Solution: `ChildShould`. Possible answers:
22
+
```
23
+
"Child#privateMethod Child#protectedMethod Child#publicMethod" // a
24
+
"Parent#privateMethod Child#protectedMethod Child#publicMethod" // b
25
+
"Parent#privateMethod Parent#protectedMethod Child#publicMethod" // c
26
+
"Parent#privateMethod Child#protectedMethod Parent#publicMethod" // d
27
+
"Parent#privateMethod Parent#protectedMethod Parent#publicMethod" // e
28
+
// It doesn't compile // f
29
+
```
19
30
20
-
### `static`
31
+
### `static` keyword
21
32
* What is it for?
22
33
* Question:
23
-
* Which would be the output?
34
+
* Which would be the output for the following `getTotal` calls?
35
+
* Solution: `CounterShould`. Possible answers:
24
36
```java
25
37
Counter counterA =newCounter();
26
38
Counter counterB =newCounter();
@@ -35,21 +47,71 @@ counterB.increaseTotal();
35
47
36
48
counterC.increaseTotal();
37
49
38
-
counterA.getTotal(); // ???
39
-
counterB.getTotal(); // ??
40
-
counterC.getTotal():// ?
50
+
// a:
51
+
counterA.getTotal(); // 0
52
+
counterB.getTotal(); // 0
53
+
counterC.getTotal():// 0
54
+
55
+
// b:
56
+
counterA.getTotal(); // 6
57
+
counterB.getTotal(); // 6
58
+
counterC.getTotal():// 6
59
+
60
+
// c:
61
+
counterA.getTotal(); // 3
62
+
counterB.getTotal(); // 5
63
+
counterC.getTotal():// 6
64
+
65
+
// c:
66
+
counterA.getTotal(); // 6
67
+
counterB.getTotal(); // 3
68
+
counterC.getTotal():// 1
41
69
```
42
-
* Solution: `CounterShould`
43
70
44
-
### `final`
45
-
* What does it do in methods and classes?
71
+
### `final` keyword
72
+
* What does it do in attributes?
73
+
* Does not allow to redefine them
74
+
* What does it do in methods?
75
+
* Does not allow to override them
76
+
* What does it do in classes?
77
+
* Does not allow to inherit from them
46
78
* When we should use it?
79
+
* Same reasoning as with the visibility keywords: Guilt presumption.
80
+
* Why: Make the next developer think twice before extending from it.
81
+
* Key concept: [Composition over Inheritance](https://medium.com/humans-create-software/composition-over-inheritance-cb6f88070205).
47
82
48
-
### `abstract` vs `interface`
83
+
### `abstract`classes vs `interface`s
49
84
* What's the difference?
85
+
* Interfaces:
86
+
* Doesn't allow to implement method bodies. It only allow us to declare method contracts/headers. <- True until Java8
87
+
* They're great because as they have fewer capabilities, they are easier to read and understand without letting us mess up adding behaviour.
88
+
* A class can implement different interfaces.
89
+
* Abstract classes:
90
+
* Allow to implement method bodies.
91
+
* A class can only extend from one abstract class.
50
92
* When we should use `abstract` classes?
93
+
* Opinion: Almost never. Just exceptional cases. We should have a very big reason to do so 🙂
51
94
* When we should use `interfaces`?
52
-
* What about multiple inheritance?
95
+
* Opinion: In order to decouple from infrastructure* stuff.
96
+
**Infrastructure: behaviour related to a third party library or component (Postgres DB, AWS SDK, Slack SDK, MailChimp API…)
0 commit comments