aboutsummaryrefslogtreecommitdiff
path: root/Info/10/2/Polymorphismus/slides.md
blob: c4b492d758be3594bf0f15ae4f47ceaa45c9b29a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
---
author: Florian Fischer
theme: Rochester
aspectratio: 169
header-includes: |
    \usepackage[school]{pgf-umlcd}
    \usepackage{unicode-math}
    \usepackage{pifont}
    \usepackage{tikz}
    \usepackage{minted}
    \usetikzlibrary{calc}
    \pgfdeclareradialshading[droplet color]{droplet}{\pgfqpoint{-10bp}{-10bp}}{color(0bp)=(droplet color!50!white); color(9bp)=(droplet color!75!white); color(18bp)=(droplet color!85!black); color(25bp)=(droplet color); color(50bp)=(droplet color!50!white)}
    \colorlet{droplet color}{blue!50!cyan}
---

## Lessons learned? AUFGABE 0

**ZEIT: 5Min**

Öffne das Bank Projekt in der Online-IDE.
Mach Dich insbesondere mit den Attributen der Klasse `Bank` sowie `Konto` vertraut.
Welche Probleme entstehen durch die fehlenden Zugriffsmodifikatoren?

## Lessons learned?

::: columns
:::: {.column}
![](../Datenkapselung/img/Computer-hacker.jpg)
::::
:::: column
![](../Datenkapselung/img/Bank.png)
::::
:::

## Erkenntnis

Auch in Software müssen, wie in der Realität, Zugriff fest geregelt sein.

\vspace{1cm}
\pause

### Dazu bieten Java die Zugrifsmodifikatoren
* `private`{.java} - Nur Zugriff innerhalb der selben Klasse
* `public`{.java} - Uneingeschränkter Zugriff

\pause

\begin{tikzpicture}[overlay, remember picture]
  \node[rotate=-20,scale=3] at (12, 3) {\Huge \color{red} \textbf{!}};
\end{tikzpicture}

## Lessons learned? AUFGABE 1

**ZEIT: 3Min**

Ergänze die Attribute der Klasse `Bank` und `Konto` um den Zugriffsmodifikator `private`{.java}.

### Tipp: Syntax Deklaration von Attributden
`<Zugriffsmodifikator> <Datentype> <Attributname>;`

## Grundsatz der Datenkapselung

Zwei Aspekte sind wichtig:

\vspace{0.5cm}

> 1. **Nur** Objekt selbst sollte Zugriff auf seine Daten haben
> 2. **Benötigter** Zugriff von außen soll über von dem Objekt bereitgestellte Methoden erfolgen

\begin{tikzpicture}[overlay, remember picture]
  \only<3> {
  \node[rotate=-30,scale=3] at (12, 3) {\Huge \color{red} \textbf{!}};
  }
\end{tikzpicture}

## Datenkapselung Anwendung {.t}

```{.java .linenos}
class Bankkonto {
  public double kontostand;
  private kontonummer;

  public gibBetrag() { return kontostand; }
  private gibKontoNummer() { return kontonummer; }
}
```

## Datenkapselung Anwendung {.t}

```{.java .linenos highlightlines=\{2\}}
class Bankkonto {
  public double kontostand;
  private kontonummer;

  public gibBetrag() { return kontostand; }
  private gibKontoNummer() { return kontonummer; }
}
```

## Datenkapselung Anwendung {.t}

```{.java .linenos highlightlines=\{2\}}
class Bankkonto {
  public double kontostand;
  private kontonummer;

  public gibBetrag() { return kontostand; }
  private gibKontoNummer() { return kontonummer; }
}
```

\vspace{1cm}

* Zeile 2: Uneingeschränkter Zugriff

## Datenkapselung Anwendung {.t}

```{.java .linenos highlightlines=\{2,6\}}
class Bankkonto {
  public double kontostand;
  private kontonummer;

  public gibBetrag() { return kontostand; }
  private gibKontoNummer() { return kontonummer; }
}
```

\vspace{1cm}

* Zeile 2: Uneingeschränkter Zugriff

## Datenkapselung Anwendung {.t}

```{.java .linenos highlightlines=\{2,6\}}
class Bankkonto {
  public double kontostand;
  private kontonummer;

  public gibBetrag() { return kontostand; }
  private gibKontoNummer() { return kontonummer; }
}
```

\vspace{1cm}

* Zeile 2: Uneingeschränkter Zugriff
* Zeile 6: Kein Zugriff von außen möglich

## Lessons learned!

::: columns
:::: {.column}
![](../Datenkapselung/img/Computer-hacker.jpg)
::::
:::: column
![](../Datenkapselung/img/Bank.png)
::::
:::

\begin{tikzpicture}[
  overlay,
  remember picture,
  raindrop/.pic={
    code={\tikzset{scale=1/10}
      \shade [shading=droplet]
      (0,0)  .. controls ++(0,-1) and ++(0,1) .. (1,-2)
      arc (360:180:1)
      .. controls ++(0,1) and ++(0,-1) .. (0,0) -- cycle;
    }
}]
  \only<2-> {
    \path (3.65,3) pic {raindrop};
    \path (3.85,2.8) pic {raindrop};
  }

  \draw[transform canvas={shift={(8,1)}}, scale=2, line width=2pt] (3ex,2.5ex) -- (1.5ex,3ex) -- (0ex,2.5ex) to [out=-90, in=165] (1.5ex,0ex) to [out=15, in=-90] cycle (1.5ex,1.5ex) node {\Large \ding{51}};
\end{tikzpicture}

## Wdh. Vererbung - Auto

![](img/Copyrighted/car_explosion.png){.center height=85%}

## Wdh. Vererbung - Auto

\begin{tikzpicture}
  \begin{class}[text width=3cm]{AutO}{0,0}
    \attribute{...}
    \operation{...}
  \end{class}

  \begin{class}[text width=3cm]{Bauteil}{9,0}
    \attribute{...}
    \operation{...}
  \end{class}

  \association{Auto}{1}{besteht aus >}{Bauteil}{< verbaut in}{n}
\end{tikzpicture}

\vspace{1cm}
Hilft uns diese Modellierung beim Konstruieren eines Autos?

## Wdh. Vererbung Spezialisierung - Auto

\begin{tikzpicture}
  \begin{class}[text width=3cm]{Auto}{0, 2.5}
    \attribute{...}
    \operation{...}
  \end{class}

  \begin{class}[text width=3cm]{Bauteil}{5, 2.5}
    \attribute{...}
    \operation{...}
  \end{class}

  \association{Auto}{1}{}{Bauteil}{n}{}

  \begin{class}[text width=4cm]{Motor}{10, 0}
    \inherit{Bauteil}
    \attribute{...}
    \operation{...}
  \end{class}

  \begin{class}[text width=4cm]{Felge}{10, 2.5}
    \inherit{Bauteil}
    \attribute{...}
    \operation{...}
  \end{class}

  \begin{class}[text width=4cm]{Windschutzscheibe}{10, 5}
    \inherit{Bauteil}
    \attribute{...}
    \operation{...}
  \end{class}
\end{tikzpicture}

## Wdh. Vererbung in Java {.t}

Das Java Schlüsselwort ...


## Wdh. Vererbung in Java {.t}

Das Java Schlüsselwort `extends`{.java} drückt eine Vererbungsbeziehung zwischen zwei Klassen aus.

\vspace{0.5cm}

\pause

::: {.block}
### Syntax der Klassendefinition
`class Unterklasse extends Oberklasse {...}`{.java}
:::

\vspace{0.5cm}

\pause

Die Unterklasse erbt alle Methoden und Attribute der Oberklasse.  
Man spricht: "Eine Felge *ist* ein Bauteil".

\pause

\begin{tikzpicture}[overlay, remember picture]
  \node[rotate=-30,scale=3] at (12, 3) {\Huge \color{red} \textbf{!}};
\end{tikzpicture}

## Wdh. Vererbung in Java

::: columns
:::: {.column width=40%}
```java
class Oberklasse {
  private int zahl;

  public void Oberklasse(int n)
  {
    zahl = n;
  }

  public void gibZahl() {
    return zahl;
  }
}
```
::::

:::: {.column width=50%}
```java
class Unterklasse extends Oberklasse {
  public void Unterklasse(int n) {
    // Oberklasse Initialisierung?
  }
}
```
::::
:::

## Wdh. Vererbung in Java

::: columns
:::: {.column width=40%}
```java
class Oberklasse {
  private int zahl;

  public void Oberklasse(int n)
  {
    zahl = n;
  }

  public void gibZahl() {
    return zahl;
  }
}
```
::::

:::: {.column width=50%}
```{.java highlightlines=\{3\}}
class Unterklasse extends Oberklasse {
  public void Unterklasse(int n) {
    super(n);
  }
}
```
::::
:::

## Wdh. Vererbung in Java {.t}

\vspace{1cm}

Die Oberklasse ist über das Java Schlüsselwort ... benutzbar.

## Wdh. Vererbung in Java {.t}

\vspace{1cm}

Die Oberklasse ist über das Java Schlüsselwort `super`{.java} benutzbar.

\vspace{1cm}
\pause

### Beispiele

* `super(...)`{.java} Konstruktoraufruf der Oberklasse
* `super.zahl`{.java} Attribut der Oberklasse
* `super.zahlGeben()`{.java} Methodenaufruf der Oberklasse

\pause

\begin{tikzpicture}[overlay, remember picture]
  \node[rotate=-30,scale=3] at (12, 3) {\Huge \color{red} \textbf{!}};
\end{tikzpicture}

## Vererbung - Problem

\begin{tikzpicture}
  \begin{class}[text width=4cm]{Sparkonto}{0,0}
    \attribute{kontostand}
    \attribute{kontonummer}
    \attribute{zinssatz}
    \operation{Einzahlen(betrag)}
    \operation{Abheben(betrag)}
    \operation{KontostandGeben()}
    \operation{Verzinsen()}
  \end{class}

  \begin{class}[text width=4cm]{Girokonto}{5,0}
    \attribute{kontostand}
    \attribute{kontonummer}
    \operation{Einzahlen(betrag)}
    \operation{Abheben(betrag)}
    \operation{KontostandGeben()}
  \end{class}

  \begin{class}[text width=4cm]{Firmenkonto}{10,0}
    \attribute{kontostand}
    \attribute{kontonummer}
    \attribute{dispokredit}
    \operation{Einzahlen(betrag)}
    \operation{Abheben(betrag)}
    \operation{KontostandGeben()}
    \operation{DispokreditSetzen(wert)}
  \end{class}
\end{tikzpicture}

\pause

Mögliche Probleme dieser Modellierung?

## Vererbung - Generalisierung

\resizebox{!}{0.5\textheight}{
\begin{tikzpicture}
  \begin{class}[text width=4cm]{Sparkonto}{0,0}
    \attribute{kontostand}
    \attribute{kontonummer}
    \attribute{zinssatz}
    \operation{Einzahlen(betrag)}
    \operation{Abheben(betrag)}
    \operation{KontostandGeben()}
    \operation{Verzinsen()}
  \end{class}

  \begin{class}[text width=4cm]{Girokonto}{5,0}
    \attribute{kontostand}
    \attribute{kontonummer}
    \operation{Einzahlen(betrag)}
    \operation{Abheben(betrag)}
    \operation{KontostandGeben()}
  \end{class}

  \begin{class}[text width=4cm]{Firmenkonto}{10,0}
    \attribute{kontostand}
    \attribute{kontonummer}
    \attribute{dispokredit}
    \operation{Einzahlen(betrag)}
    \operation{Abheben(betrag)}
    \operation{KontostandGeben()}
    \operation{DispokreditSetzen(wert)}
  \end{class}
\end{tikzpicture}
}

**Idee**: Auslagern der gemeinsamen Attribute und Methoden in eine Oberklasse

**Arbeitsauftrag 3min**: Zeichne ein Klassendiagramm mit einer gemeinsamen Oberklasse

## Generalisierung - KONTO

\begin{tikzpicture}
  \node[rotate=30,scale=3] at (0, 3) {\Huge \color{red} \textbf{!}};

  \begin{class}[text width=4cm]{KONTO}{5,4}
    \attribute{kontostand}
    \attribute{kontonummer}
    \operation{Einzahlen(betrag)}
    \operation{Abheben(betrag)}
    \operation{KontostandGeben()}
  \end{class}

  \begin{class}[text width=4cm]{SPARKONTO}{0,0}
    \inherit{KONTO}
    \attribute{zinssatz}
    \operation{Verzinsen()}
  \end{class}


  \begin{class}[text width=4cm]{GIROKONTO}{5,0}
    \inherit{KONTO}
  \end{class}

  \begin{class}[text width=4cm]{FIRMENKONTO}{10,0}
    \inherit{KONTO}
    \attribute{dispokredit}
    \operation{DispokreditSetzen(wert)}
  \end{class}
\end{tikzpicture}

## Generalisierung - Java

\small

::: columns
:::: {.column width=50%}
```java
class Konto {
  private int kontostand;
  private int kontonummer;

  public void Konto(int kNr) {
    kontostand = 0;
    kontonummer = kNr;
  }

  public void Einzahlen(int betrag) {
  ...
}
```
::::

:::: {.column width=50%}
```java
// Klassendefinition ?
  private double zinssatz;

  public void Sparkonto(int kNr,
                        double zs) {
    // KONTO Initialisierung?
    zinssatz = zs;
  }

  public void Verzinsen() {
  ...
}
```
::::
:::

## Generalisierung - Java

\small

::: columns
:::: {.column width=50%}
```java
class Konto {
  private int kontostand;
  private int kontonummer;

  public void Konto(int kNr) {
    kontostand = 0;
    kontonummer = kNr;
  }

  public void Einzahlen(int betrag) {
  ...
}
```
::::

:::: {.column width=50%}
```{.java highlightlines=\{1\}}
class Sparkonto extends Konto {
  private double zinssatz;

  public void Sparkonto(int kNr,
                        double zs) {
    super(kNr);
    zinssatz = zs;
  }

  public void Verzinsen() {
  ...
}
```
::::
:::

## Generalisierung - Java

\small

::: columns
:::: {.column width=50%}
```java
class Konto {
  private int kontostand;
  private int kontonummer;

  public void Konto(int kNr) {
    kontostand = 0;
    kontonummer = kNr;
  }

  public void Einzahlen(int betrag) {
  ...
}
```
::::

:::: {.column width=50%}
```{.java highlightlines=\{1,6\}}
class Sparkonto extends KONTO {
  private double zinssatz;

  public void Sparkonto(int kNr,
                        double zs) {
    super(kNr);
    zinssatz = zs;
  }

  public void Verzinsen() {
  ...
}
```
::::
:::

## Polymorphismus

\begin{center}
griechisch Vielgestaltigkeit
\end{center}

![](img/wolpertinger){.center}

## Arbeitsauftrag Tierheimverwaltung

* **AUFGABE 0:** Mache dich kurz mit dem Projekt Tierheim in der Online-IDE vertraut
* **AUFGABE 1:** Überlege dir wie du unterschiedliche Tiere im Tierheim speicherst und setze deine Überlegung um
* **AUFGABE 2:** Begründe warum Pluto (Objekt der Klasse Hund) nicht ins Tierheim aufgenommen werden kann

## Polymorphismus

*... ist die Eigenschaft, dass ein **Referenzattribut** nicht nur Objekte der als Datentyp angegebenen Klasse referenzieren kann, sondern auch Objekte **aller** zu dieser Klasse existierenden **Unterklassen**.*

## Polymorphismus

\begin{tikzpicture}[scale=0.5]
  \begin{object}[text width=3.5cm]{th1}{0,0.5}
    \instanceOf{Tierheim}
    \attribute{tiere[0]}
    \attribute{tiere[1]}
    \attribute{...}
  \end{object}

  \begin{object}[text width=4cm]{h1}{12,0}
    \instanceOf{Tier}
    \attribute{name="Wolle"}
    \attribute{geschlecht="ungewiss"}
  \end{object}

  \begin{object}[text width=4cm]{t1}{12,5}
    \instanceOf{Hund}
    \attribute{name="Pluto" }
    \attribute{geschlecht="maennlich"}
    \attribute{rasse="comic"}
  \end{object}

  \only<2->{
    \node[fill=black, circle, minimum size=0.075cm] (circle) at (2.2, -2.35) {};
    \draw[line width=2pt, -latex] (circle) edge [bend right=15] (8, -0.5) {};
  }
  \only<3> {
    \node[fill=black, circle, minimum size=0.075cm] (circle2) at (2.2, -1.4) {};
    \draw[line width=2pt, -latex] (circle2) edge [bend left=30] (8, 4.5) {};
  }
\end{tikzpicture}

## Arbeitsauftrag Tierheimverwaltung Fortsetzung

* **AUFGABE 3:** Vervollständige die Klasse Hund als Spezialisierung der Klasse Tier
* **AUFGABE 4:** Implementiere die Methode `Tierheim.besuchen()` und überlege dir welche Ausgaben das Programm machen wird. Überprüfe deine Vermutung in dem du das Programm ausführst.

## Polymorphismus

*ist die Eigenschaft, dass ein **Referenzattribut** nicht nur Objekte der als Datentyp angegebenen Klasse referenzieren kann, sondern auch Objekte **aller** zu dieser Klasse existierenden **Unterklassen**.*

\vspace{0.25cm}

*Wird ein **Objekt einer Unterklasse** über dieses Referenzattribut referenziert, so werden Attribute und Methoden verwendet, wie sie in **seiner Klasse** bekannt sind (und nicht die überschriebenen Methoden der Oberklasse).*

\pause

\begin{tikzpicture}[overlay, remember picture]
  \node[rotate=-20,scale=3] at (12, -0.5) {\Huge \color{red} \textbf{!}};
\end{tikzpicture}

## Beispiel

Was gibt dieses Programm aus?

```java
Tier pluto = new Hund("pluto", "maennlich", "comic");
pluto.gibLaut();
```

\pause

::: {.block}
### Ausgabe
`Wuff`
:::

Da der Polymorphismus dafür sorgt das die Methode wie sie in der Unterklasse `Hund` bekannt ist ausgeführt wird.