Skip to content

Commit 94a5858

Browse files
committed
make examples compiles, reformat a little
1 parent 2d00edb commit 94a5858

File tree

3 files changed

+46
-58
lines changed

3 files changed

+46
-58
lines changed

chapter15-inheritance.jsh

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ class B extends A { // I want to reuse and augment the definition of A
1515
}
1616

1717
// Precisely, inheritance is 3 different things grouped together
18-
// - _subtyping_
19-
// everywhere something is typed `A`, you can send a `B` instead
20-
// - _members inheritance_
21-
// all instance members of `A` are copied in `B`
22-
// - _polymorphism_
23-
// you can replace the code of a method from `A` to adapt it to `B`
18+
// - __subtyping__
19+
// everywhere something is typed `A`, you can send a `B` instead
20+
// - __members inheritance__
21+
// all instance members of `A` are copied in `B`
22+
// - __polymorphism__
23+
// you can replace the code of a method from `A` to adapt it to `B`
2424

2525
// ## Subtyping
2626
// Subtyping is the most important part of the inheritance, it allows to reuse
@@ -42,15 +42,15 @@ class B extends A { // so B is a subtype of A
4242
var b = new B();
4343
sayHello(b);
4444

45-
// _subtyping_ is very important, because it means that we can reuse a
45+
// __subtyping__ is very important, because it means that we can reuse a
4646
// method by calling it with several different types. And given that,
4747
// The more a method is used, the less buggy it is, _subtyping_ helps
4848
// to make applications more robust by sharing methods.
4949

5050

5151
// ## Polymorphism
52-
// _Polymorphism_ works with _subtyping_, _subtyping_ allow to call a
53-
// code with a subtype of the declared type. _Polymorphism_ allows to
52+
// Polymorphism works with __subtyping__, __subtyping__ allow to call a
53+
// code with a subtype of the declared type. Polymorphism allows to
5454
// adapt parts of the shared code to the subclass at runtime.
5555

5656
// By example, let suppose we have a class able to 'enhance' a text
@@ -76,22 +76,22 @@ var enhancer = new StarEnhancer();
7676
sayHello(enhancer, "polymorphism");
7777

7878

79-
// So not only we can call `sayHello()` with a `StarEnhancer` (_subtyping_),
79+
// So not only we can call `sayHello()` with a `StarEnhancer` (__subtyping__),
8080
// but inside `sayHello()`, the method call to `enhance()` will call
8181
// the methode `StarEnhancer.enhance()` adapting the code of `hello()`
8282
// to the fact that at runtime the enhancer is in fact a `StarEnhancer`.
8383

8484
// The mechanism that choose the `right` method in function of the object
85-
// at runtime is called _polymorphism_.
85+
// at runtime is called __polymorphism__.
8686

8787

8888
// ### Overriding
8989
// In the example above, `enhancer.enhance()` inside the method `sayHello()`
9090
// can call `Enhancer.enhance()` or `StarEnhancer.enhance()`.
91-
// We say that the method `enhance()` of `StarEnhancer` _overrides_
91+
// We say that the method `enhance()` of `StarEnhancer` __overrides__
9292
// the method `enhance()` of `Enhancer`.
9393

94-
// A method to _override_ another has to
94+
// A method to __override__ another has to
9595
// - have the same name
9696
// - have the same number of parameter
9797
// - can have a subtype as return type
@@ -154,7 +154,6 @@ class Animal {
154154
}
155155
class Lion extends Animal {
156156
boolean young;
157-
158157
void roar() {
159158
System.out.println(name + " roar");
160159
}
@@ -266,7 +265,7 @@ var palace = new Palace(100, 50);
266265
System.out.println(palace.price(2));
267266

268267

269-
// ### field protected
268+
// ### Field protected
270269
// In the code above, one can use the modifier `protected` too but
271270
// because a `protected` field is visible by any subclass even the one
272271
// the author of the subclass do not control. It means that the field
@@ -277,7 +276,7 @@ System.out.println(palace.price(2));
277276

278277
// ## Relation with interfaces
279278
// Nowadays, inheritance is used less and less in Java because interface
280-
// provides _subtyping_ and _overriding_ without _members inheritance_.
279+
// provides __subtyping__ and __overriding__ without __members inheritance__.
281280
// Given that the later mechanism is the one causing trouble,
282281
// using an interface is often preferred to using inheritance.
283282

@@ -308,7 +307,7 @@ System.out.println(palace.price(2));
308307

309308
// ## Use delegation not inheritance
310309
// Sometimes people are using inheritance where they should not !
311-
// The worst occurrences is when people want _members inheritance_
310+
// The worst occurrences is when people want __members inheritance__
312311
// to avoid to write too many methods but forget that they get
313312
// all the methods even the one they don't want.
314313

@@ -353,7 +352,7 @@ class Properties {
353352
}
354353
}
355354
var properties = new Properties();
356-
properties.setProperty("java", "best language ever");
355+
properties.setProperty("java", "best language ever, for life !");
357356
System.out.println(properties.getProperty("java", "??"));
358357
System.out.println(properties.getProperty("brainfuck", "??"));
359358

guide/chapter15-inheritance.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ class B extends A { // I want to reuse and augment the definition of A
1414
```
1515

1616
Precisely, inheritance is 3 different things grouped together
17-
- _subtyping_
18-
everywhere something is typed `A`, you can send a `B` instead
19-
- _members inheritance_
20-
all instance members of `A` are copied in `B`
21-
- _polymorphism_
22-
you can replace the code of a method from `A` to adapt it to `B`
17+
- __subtyping__
18+
everywhere something is typed `A`, you can send a `B` instead
19+
- __members inheritance__
20+
all instance members of `A` are copied in `B`
21+
- __polymorphism__
22+
you can replace the code of a method from `A` to adapt it to `B`
2323

2424
## Subtyping
2525
Subtyping is the most important part of the inheritance, it allows to reuse
@@ -45,15 +45,15 @@ var b = new B();
4545
sayHello(b);
4646
```
4747

48-
_subtyping_ is very important, because it means that we can reuse a
48+
__subtyping__ is very important, because it means that we can reuse a
4949
method by calling it with several different types. And given that,
5050
The more a method is used, the less buggy it is, _subtyping_ helps
5151
to make applications more robust by sharing methods.
5252

5353

5454
## Polymorphism
55-
_Polymorphism_ works with _subtyping_, _subtyping_ allow to call a
56-
code with a subtype of the declared type. _Polymorphism_ allows to
55+
Polymorphism works with __subtyping__, __subtyping__ allow to call a
56+
code with a subtype of the declared type. Polymorphism allows to
5757
adapt parts of the shared code to the subclass at runtime.
5858

5959
By example, let suppose we have a class able to 'enhance' a text
@@ -83,22 +83,22 @@ sayHello(enhancer, "polymorphism");
8383
```
8484

8585

86-
So not only we can call `sayHello()` with a `StarEnhancer` (_subtyping_),
86+
So not only we can call `sayHello()` with a `StarEnhancer` (__subtyping__),
8787
but inside `sayHello()`, the method call to `enhance()` will call
8888
the methode `StarEnhancer.enhance()` adapting the code of `hello()`
8989
to the fact that at runtime the enhancer is in fact a `StarEnhancer`.
9090

9191
The mechanism that choose the `right` method in function of the object
92-
at runtime is called _polymorphism_.
92+
at runtime is called __polymorphism__.
9393

9494

9595
### Overriding
9696
In the example above, `enhancer.enhance()` inside the method `sayHello()`
9797
can call `Enhancer.enhance()` or `StarEnhancer.enhance()`.
98-
We say that the method `enhance()` of `StarEnhancer` _overrides_
98+
We say that the method `enhance()` of `StarEnhancer` __overrides__
9999
the method `enhance()` of `Enhancer`.
100100

101-
A method to _override_ another has to
101+
A method to __override__ another has to
102102
- have the same name
103103
- have the same number of parameter
104104
- can have a subtype as return type
@@ -166,9 +166,6 @@ class Animal {
166166
}
167167
class Lion extends Animal {
168168
boolean young;
169-
```
170-
171-
```java
172169
void roar() {
173170
System.out.println(name + " roar");
174171
}
@@ -289,7 +286,7 @@ System.out.println(palace.price(2));
289286
```
290287

291288

292-
### field protected
289+
### Field protected
293290
In the code above, one can use the modifier `protected` too but
294291
because a `protected` field is visible by any subclass even the one
295292
the author of the subclass do not control. It means that the field
@@ -300,7 +297,7 @@ can not be changed the same way a `public` field can not be changed.
300297

301298
## Relation with interfaces
302299
Nowadays, inheritance is used less and less in Java because interface
303-
provides _subtyping_ and _overriding_ without _members inheritance_.
300+
provides __subtyping__ and __overriding__ without __members inheritance__.
304301
Given that the later mechanism is the one causing trouble,
305302
using an interface is often preferred to using inheritance.
306303

@@ -333,7 +330,7 @@ System.out.println(palace.price(2));
333330

334331
## Use delegation not inheritance
335332
Sometimes people are using inheritance where they should not !
336-
The worst occurrences is when people want _members inheritance_
333+
The worst occurrences is when people want __members inheritance__
337334
to avoid to write too many methods but forget that they get
338335
all the methods even the one they don't want.
339336

@@ -381,7 +378,7 @@ class Properties {
381378
}
382379
}
383380
var properties = new Properties();
384-
properties.setProperty("java", "best language ever");
381+
properties.setProperty("java", "best language ever, for life !");
385382
System.out.println(properties.getProperty("java", "??"));
386383
System.out.println(properties.getProperty("brainfuck", "??"));
387384
```

jupyter/chapter15-inheritance.ipynb

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
{
3535
"cell_type": "markdown",
3636
"metadata": {},
37-
"source": ["Precisely, inheritance is 3 different things grouped together \n", "- _subtyping_\n", " everywhere something is typed `A`, you can send a `B` instead\n", "- _members inheritance_\n", " all instance members of `A` are copied in `B`\n", "- _polymorphism_\n", " you can replace the code of a method from `A` to adapt it to `B`\n"]
37+
"source": ["Precisely, inheritance is 3 different things grouped together \n", "- __subtyping__\n", " everywhere something is typed `A`, you can send a `B` instead\n", "- __members inheritance__\n", " all instance members of `A` are copied in `B`\n", "- __polymorphism__\n", " you can replace the code of a method from `A` to adapt it to `B`\n"]
3838
}
3939
,
4040
{
@@ -74,13 +74,13 @@
7474
{
7575
"cell_type": "markdown",
7676
"metadata": {},
77-
"source": ["_subtyping_ is very important, because it means that we can reuse a\n", "method by calling it with several different types. And given that,\n", "The more a method is used, the less buggy it is, _subtyping_ helps\n", "to make applications more robust by sharing methods.\n"]
77+
"source": ["__subtyping__ is very important, because it means that we can reuse a\n", "method by calling it with several different types. And given that,\n", "The more a method is used, the less buggy it is, _subtyping_ helps\n", "to make applications more robust by sharing methods.\n"]
7878
}
7979
,
8080
{
8181
"cell_type": "markdown",
8282
"metadata": {},
83-
"source": ["## Polymorphism\n", "_Polymorphism_ works with _subtyping_, _subtyping_ allow to call a\n", "code with a subtype of the declared type. _Polymorphism_ allows to\n", "adapt parts of the shared code to the subclass at runtime.\n"]
83+
"source": ["## Polymorphism\n", "Polymorphism works with __subtyping__, __subtyping__ allow to call a\n", "code with a subtype of the declared type. Polymorphism allows to\n", "adapt parts of the shared code to the subclass at runtime.\n"]
8484
}
8585
,
8686
{
@@ -114,25 +114,25 @@
114114
{
115115
"cell_type": "markdown",
116116
"metadata": {},
117-
"source": ["So not only we can call `sayHello()` with a `StarEnhancer` (_subtyping_),\n", "but inside `sayHello()`, the method call to `enhance()` will call\n", "the methode `StarEnhancer.enhance()` adapting the code of `hello()`\n", "to the fact that at runtime the enhancer is in fact a `StarEnhancer`.\n"]
117+
"source": ["So not only we can call `sayHello()` with a `StarEnhancer` (__subtyping__),\n", "but inside `sayHello()`, the method call to `enhance()` will call\n", "the methode `StarEnhancer.enhance()` adapting the code of `hello()`\n", "to the fact that at runtime the enhancer is in fact a `StarEnhancer`.\n"]
118118
}
119119
,
120120
{
121121
"cell_type": "markdown",
122122
"metadata": {},
123-
"source": ["The mechanism that choose the `right` method in function of the object\n", "at runtime is called _polymorphism_.\n"]
123+
"source": ["The mechanism that choose the `right` method in function of the object\n", "at runtime is called __polymorphism__.\n"]
124124
}
125125
,
126126
{
127127
"cell_type": "markdown",
128128
"metadata": {},
129-
"source": ["### Overriding\n", "In the example above, `enhancer.enhance()` inside the method `sayHello()`\n", "can call `Enhancer.enhance()` or `StarEnhancer.enhance()`.\n", "We say that the method `enhance()` of `StarEnhancer` _overrides_\n", "the method `enhance()` of `Enhancer`.\n"]
129+
"source": ["### Overriding\n", "In the example above, `enhancer.enhance()` inside the method `sayHello()`\n", "can call `Enhancer.enhance()` or `StarEnhancer.enhance()`.\n", "We say that the method `enhance()` of `StarEnhancer` __overrides__\n", "the method `enhance()` of `Enhancer`.\n"]
130130
}
131131
,
132132
{
133133
"cell_type": "markdown",
134134
"metadata": {},
135-
"source": ["A method to _override_ another has to\n", "- have the same name\n", "- have the same number of parameter\n", "- can have a subtype as return type\n", "- can have subtypes of the declared exceptions (`throws`). \n"]
135+
"source": ["A method to __override__ another has to\n", "- have the same name\n", "- have the same number of parameter\n", "- can have a subtype as return type\n", "- can have subtypes of the declared exceptions (`throws`). \n"]
136136
}
137137
,
138138
{
@@ -186,15 +186,7 @@
186186
"execution_count": null,
187187
"metadata": {},
188188
"outputs": [],
189-
"source": ["class Animal {\n", " String name;\n", "}\n", "class Lion extends Animal {\n", " boolean young;\n"]
190-
}
191-
,
192-
{
193-
"cell_type": "code",
194-
"execution_count": null,
195-
"metadata": {},
196-
"outputs": [],
197-
"source": [" void roar() {\n", " System.out.println(name + \" roar\");\n", " }\n", "}\n", "var lion = new Lion();\n", "lion.name = \"leo\";\n", "lion.young = true;\n", "lion.roar();\n"]
189+
"source": ["class Animal {\n", " String name;\n", "}\n", "class Lion extends Animal {\n", " boolean young;\n", " void roar() {\n", " System.out.println(name + \" roar\");\n", " }\n", "}\n", "var lion = new Lion();\n", "lion.name = \"leo\";\n", "lion.young = true;\n", "lion.roar();\n"]
198190
}
199191
,
200192
{
@@ -286,7 +278,7 @@
286278
{
287279
"cell_type": "markdown",
288280
"metadata": {},
289-
"source": ["### field protected\n", "In the code above, one can use the modifier `protected` too but\n", "because a `protected` field is visible by any subclass even the one\n", "the author of the subclass do not control. It means that the field\n", "can not be changed the same way a `public` field can not be changed.\n"]
281+
"source": ["### Field protected\n", "In the code above, one can use the modifier `protected` too but\n", "because a `protected` field is visible by any subclass even the one\n", "the author of the subclass do not control. It means that the field\n", "can not be changed the same way a `public` field can not be changed.\n"]
290282
}
291283
,
292284
{
@@ -298,7 +290,7 @@
298290
{
299291
"cell_type": "markdown",
300292
"metadata": {},
301-
"source": ["## Relation with interfaces\n", "Nowadays, inheritance is used less and less in Java because interface\n", "provides _subtyping_ and _overriding_ without _members inheritance_.\n", "Given that the later mechanism is the one causing trouble,\n", "using an interface is often preferred to using inheritance.\n"]
293+
"source": ["## Relation with interfaces\n", "Nowadays, inheritance is used less and less in Java because interface\n", "provides __subtyping__ and __overriding__ without __members inheritance__.\n", "Given that the later mechanism is the one causing trouble,\n", "using an interface is often preferred to using inheritance.\n"]
302294
}
303295
,
304296
{
@@ -324,7 +316,7 @@
324316
{
325317
"cell_type": "markdown",
326318
"metadata": {},
327-
"source": ["## Use delegation not inheritance\n", "Sometimes people are using inheritance where they should not !\n", "The worst occurrences is when people want _members inheritance_\n", "to avoid to write too many methods but forget that they get\n", "all the methods even the one they don't want.\n"]
319+
"source": ["## Use delegation not inheritance\n", "Sometimes people are using inheritance where they should not !\n", "The worst occurrences is when people want __members inheritance__\n", "to avoid to write too many methods but forget that they get\n", "all the methods even the one they don't want.\n"]
328320
}
329321
,
330322
{
@@ -370,7 +362,7 @@
370362
"execution_count": null,
371363
"metadata": {},
372364
"outputs": [],
373-
"source": ["class Properties {\n", " private final HashMap<String, String> map = new HashMap<>();\n", " public String getProperty(String key, String defaultValue) {\n", " Objects.requireNonNull(key);\n", " return map.getOrDefault(key, defaultValue);\n", " }\n", " public void setProperty(String key, String value) {\n", " Objects.requireNonNull(key);\n", " Objects.requireNonNull(value);\n", " map.put(key, value);\n", " }\n", "}\n", "var properties = new Properties();\n", "properties.setProperty(\"java\", \"best language ever\");\n", "System.out.println(properties.getProperty(\"java\", \"??\"));\n", "System.out.println(properties.getProperty(\"brainfuck\", \"??\"));\n"]
365+
"source": ["class Properties {\n", " private final HashMap<String, String> map = new HashMap<>();\n", " public String getProperty(String key, String defaultValue) {\n", " Objects.requireNonNull(key);\n", " return map.getOrDefault(key, defaultValue);\n", " }\n", " public void setProperty(String key, String value) {\n", " Objects.requireNonNull(key);\n", " Objects.requireNonNull(value);\n", " map.put(key, value);\n", " }\n", "}\n", "var properties = new Properties();\n", "properties.setProperty(\"java\", \"best language ever, for life !\");\n", "System.out.println(properties.getProperty(\"java\", \"??\"));\n", "System.out.println(properties.getProperty(\"brainfuck\", \"??\"));\n"]
374366
}
375367
,
376368
{

0 commit comments

Comments
 (0)