|
34 | 34 | {
|
35 | 35 | "cell_type": "markdown",
|
36 | 36 | "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"] |
38 | 38 | }
|
39 | 39 | ,
|
40 | 40 | {
|
|
74 | 74 | {
|
75 | 75 | "cell_type": "markdown",
|
76 | 76 | "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"] |
78 | 78 | }
|
79 | 79 | ,
|
80 | 80 | {
|
81 | 81 | "cell_type": "markdown",
|
82 | 82 | "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"] |
84 | 84 | }
|
85 | 85 | ,
|
86 | 86 | {
|
|
114 | 114 | {
|
115 | 115 | "cell_type": "markdown",
|
116 | 116 | "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"] |
118 | 118 | }
|
119 | 119 | ,
|
120 | 120 | {
|
121 | 121 | "cell_type": "markdown",
|
122 | 122 | "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"] |
124 | 124 | }
|
125 | 125 | ,
|
126 | 126 | {
|
127 | 127 | "cell_type": "markdown",
|
128 | 128 | "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"] |
130 | 130 | }
|
131 | 131 | ,
|
132 | 132 | {
|
133 | 133 | "cell_type": "markdown",
|
134 | 134 | "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"] |
136 | 136 | }
|
137 | 137 | ,
|
138 | 138 | {
|
|
186 | 186 | "execution_count": null,
|
187 | 187 | "metadata": {},
|
188 | 188 | "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"] |
198 | 190 | }
|
199 | 191 | ,
|
200 | 192 | {
|
|
286 | 278 | {
|
287 | 279 | "cell_type": "markdown",
|
288 | 280 | "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"] |
290 | 282 | }
|
291 | 283 | ,
|
292 | 284 | {
|
|
298 | 290 | {
|
299 | 291 | "cell_type": "markdown",
|
300 | 292 | "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"] |
302 | 294 | }
|
303 | 295 | ,
|
304 | 296 | {
|
|
324 | 316 | {
|
325 | 317 | "cell_type": "markdown",
|
326 | 318 | "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"] |
328 | 320 | }
|
329 | 321 | ,
|
330 | 322 | {
|
|
370 | 362 | "execution_count": null,
|
371 | 363 | "metadata": {},
|
372 | 364 | "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"] |
374 | 366 | }
|
375 | 367 | ,
|
376 | 368 | {
|
|
0 commit comments