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
|
<html>
<head>
<title>
Handbuch der Java-Programmierung, 5. Auflage
</title>
</head>
<body>
<a name="startofbody"></a>
<script language="JavaScript" src="hjp4lib.js">
</script>
<script language="JavaScript">
installKbdHandler("97,#startofbody;101,#endofbody;116,cover.html;122,k100003.html;115,search.html;105,index.html;100,JDKDOCS;112,APIDOCS;104,k100051.html;106,k100054.html;107,k100056.html;108,k100057.html");
</script>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100051.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100054.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100056.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100057.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">Kapitel 8 - OOP II: Vererbung, Polymorphismus und statische Elemente
</table>
<hr>
<!-- Section -->
<a name="abstraktpolymorph"></a>
<h2>8.4 <a name="ixa100543">Abstrakte Klassen und Polymorphismus</a></h2>
<hr>
<ul>
<li><a href="k100055.html#abstraktpolymorph">8.4 Abstrakte Klassen und Polymorphismus</a>
<ul>
<li><a href="k100055.html#sectlevel3id008004001">8.4.1 Abstrakte Klassen</a>
<li><a href="k100055.html#sectlevel3id008004002">8.4.2 Ein Beispiel für Polymorphismus</a>
<li><a href="k100055.html#sectlevel3id008004003">8.4.3 Polymorphe Methodenaufrufe in Konstruktoren</a>
</ul>
</ul>
<hr>
<!-- Section -->
<a name="sectlevel3id008004001"></a>
<h3>8.4.1 Abstrakte Klassen </h3>
<p>
In Java ist es möglich, <i>abstrakte</i> Methoden zu definieren.
Im Gegensatz zu den <i>konkreten</i> Methoden enthalten sie nur die
Deklaration der Methode, nicht aber ihre Implementierung. Syntaktisch
unterscheiden sich abstrakte Methoden dadurch, dass anstelle der geschweiften
Klammern mit den auszuführenden Anweisungen lediglich ein Semikolon
steht. Zusätzlich wird die Methode mit dem Attribut <a name="ixa100544"><a href="index_a.html#ixb100440"><font color=#000080><tt>abstract</tt></font></a></a>
versehen. Abstrakte Methoden können nicht aufgerufen werden,
sie definieren nur eine Schnittstelle. Erst durch Überlagerung
in einer abgeleiteten Klasse und Implementierung des fehlenden Methodenrumpfes
wird eine abstrakte Klasse konkret und kann aufgerufen werden.
<p>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Eine Klasse, die mindestens eine abstrakte Methode enthält, wird
selbst als abstrakt angesehen und muss ebenfalls mit dem Schlüsselwort
<a href="index_a.html#ixb100440"><font color=#000080><tt>abstract</tt></font></a>
versehen werden. Abstrakte Klassen können nicht instanziert werden,
da sie Methoden enthalten, die nicht implementiert wurden. Statt dessen
werden abstrakte Klassen abgeleitet, und in der abgeleiteten Klasse
werden eine oder mehrere der abstrakten Methoden implementiert. Eine
abstrakte Klasse wird konkret, wenn alle ihre Methoden implementiert
sind. Die Konkretisierung kann dabei auch schrittweise über mehrere
Vererbungsstufen erfolgen.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#000077">
<tr>
<td><font color="#FFFFFF"> Hinweis </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>
<!-- Section -->
<a name="sectlevel3id008004002"></a>
<h3>8.4.2 Ein Beispiel für Polymorphismus </h3>
<p>
Wir wollen uns ein Beispiel ansehen, um diese Ausführungen zu
verdeutlichen. Zum Aufbau einer Mitarbeiterdatenbank soll zunächst
eine Basisklasse definiert werden, die jene Eigenschaften implementiert,
die auf alle Mitarbeiter zutreffen, wie beispielsweise <i>persönliche
Daten</i> oder der <i>Eintrittstermin in das Unternehmen</i>. Gleichzeitig
soll diese Klasse als Basis für spezialisierte Unterklassen verwendet
werden, um die Besonderheiten spezieller Mitarbeitertypen, wie <i>Arbeiter</i>,
<i>Angestellte</i> oder <i>Manager</i>, abzubilden. Da die Berechnung
des monatlichen Gehalts zwar für jeden Mitarbeiter erforderlich,
in ihrer konkreten Realisierung aber abhängig vom Typ des Mitarbeiters
ist, soll eine abstrakte Methode <font color="#000077"><tt>monatsBrutto</tt></font>
in der Basisklasse definiert und in den abgeleiteten Klassen konkretisiert
werden.
<p>
Das folgende Listing zeigt die Implementierung der Klassen <font color="#000077"><tt>Mitarbeiter</tt></font>,
<font color="#000077"><tt>Arbeiter</tt></font>, <font color="#000077"><tt>Angestellter</tt></font>
und <font color="#000077"><tt>Manager</tt></font> zur Realisierung
der verschiedenen Mitarbeitertypen. Zusätzlich wird die Klasse
<font color="#000077"><tt>Gehaltsberechnung</tt></font> definiert,
um das Hauptprogramm zur Verfügung zu stellen, in dem die Gehaltsberechnung
durchgeführt wird. Dazu wird ein Array <font color="#000077"><tt>ma</tt></font>
mit konkreten Untertypen der Klasse <font color="#000077"><tt>Mitarbeiter</tt></font>
gefüllt (hier nur angedeutet; die Daten könnten zum Beispiel
aus einer Datenbank gelesen werden) und dann für alle Elemente
das Monatsgehalt durch Aufruf von <font color="#000077"><tt>monatsBrutto</tt></font>
ermittelt.
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Das Listing ist ebenfalls ein Beispiel für <a name="ixa100545"><i>Polymorphismus</i></a>.
Eine Variable vom Typ einer Basisklasse nimmt zur Laufzeit unterschiedliche
Objekte aus abgeleiteten Klassen auf. Da bereits in der Basisklasse
die Definition von <font color="#000077"><tt>monatsBrutto</tt></font>
vorgenommen wurde, akzeptiert der Compiler den Aufruf dieser Methode
bereits bei Objekten dieser vermeintlich abstrakten Klasse. Erst zur
Laufzeit ist dann bekannt, welcher abgeleitete Typ hinter jedem einzelnen
Array-Element steht, und das Laufzeitsystem ruft die darin implementierte
Variante der Methode <font color="#000077"><tt>monatsBrutto</tt></font>
auf.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#000077">
<tr>
<td><font color="#FFFFFF"> Hinweis </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>
<a name="gehaltsberechnung"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* Gehaltsberechnung.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.util.Date;
<font color="#555555">004 </font>
<font color="#555555">005 </font><font color="#0000AA">abstract</font> <font color="#0000AA">class</font> Mitarbeiter
<font color="#555555">006 </font>{
<font color="#555555">007 </font> <font color="#006699">int</font> persnr;
<font color="#555555">008 </font> String name;
<font color="#555555">009 </font> Date eintritt;
<font color="#555555">010 </font>
<font color="#555555">011 </font> <font color="#0000AA">public</font> Mitarbeiter()
<font color="#555555">012 </font> {
<font color="#555555">013 </font> }
<font color="#555555">014 </font>
<font color="#555555">015 </font> <font color="#0000AA">public</font> <font color="#0000AA">abstract</font> <font color="#006699">double</font> monatsBrutto();
<font color="#555555">016 </font>}
<font color="#555555">017 </font>
<font color="#555555">018 </font><font color="#0000AA">class</font> Arbeiter
<font color="#555555">019 </font><font color="#0000AA">extends</font> Mitarbeiter
<font color="#555555">020 </font>{
<font color="#555555">021 </font> <font color="#006699">double</font> stundenlohn;
<font color="#555555">022 </font> <font color="#006699">double</font> anzahlstunden;
<font color="#555555">023 </font> <font color="#006699">double</font> ueberstundenzuschlag;
<font color="#555555">024 </font> <font color="#006699">double</font> anzahlueberstunden;
<font color="#555555">025 </font> <font color="#006699">double</font> schichtzulage;
<font color="#555555">026 </font>
<font color="#555555">027 </font> <font color="#0000AA">public</font> <font color="#006699">double</font> monatsBrutto()
<font color="#555555">028 </font> {
<font color="#555555">029 </font> <font color="#0000AA">return</font> stundenlohn*anzahlstunden+
<font color="#555555">030 </font> ueberstundenzuschlag*anzahlueberstunden+
<font color="#555555">031 </font> schichtzulage;
<font color="#555555">032 </font> }
<font color="#555555">033 </font>}
<font color="#555555">034 </font>
<font color="#555555">035 </font><font color="#0000AA">class</font> Angestellter
<font color="#555555">036 </font><font color="#0000AA">extends</font> Mitarbeiter
<font color="#555555">037 </font>{
<font color="#555555">038 </font> <font color="#006699">double</font> grundgehalt;
<font color="#555555">039 </font> <font color="#006699">double</font> ortszuschlag;
<font color="#555555">040 </font> <font color="#006699">double</font> zulage;
<font color="#555555">041 </font>
<font color="#555555">042 </font> <font color="#0000AA">public</font> <font color="#006699">double</font> monatsBrutto()
<font color="#555555">043 </font> {
<font color="#555555">044 </font> <font color="#0000AA">return</font> grundgehalt+
<font color="#555555">045 </font> ortszuschlag+
<font color="#555555">046 </font> zulage;
<font color="#555555">047 </font> }
<font color="#555555">048 </font>}
<font color="#555555">049 </font>
<font color="#555555">050 </font><font color="#0000AA">class</font> Manager
<font color="#555555">051 </font><font color="#0000AA">extends</font> Mitarbeiter
<font color="#555555">052 </font>{
<font color="#555555">053 </font> <font color="#006699">double</font> fixgehalt;
<font color="#555555">054 </font> <font color="#006699">double</font> provision1;
<font color="#555555">055 </font> <font color="#006699">double</font> provision2;
<font color="#555555">056 </font> <font color="#006699">double</font> umsatz1;
<font color="#555555">057 </font> <font color="#006699">double</font> umsatz2;
<font color="#555555">058 </font>
<font color="#555555">059 </font> <font color="#0000AA">public</font> <font color="#006699">double</font> monatsBrutto()
<font color="#555555">060 </font> {
<font color="#555555">061 </font> <font color="#0000AA">return</font> fixgehalt+
<font color="#555555">062 </font> umsatz1*provision1/100+
<font color="#555555">063 </font> umsatz2*provision2/100;
<font color="#555555">064 </font> }
<font color="#555555">065 </font>}
<font color="#555555">066 </font>
<font color="#555555">067 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Gehaltsberechnung
<font color="#555555">068 </font>{
<font color="#555555">069 </font> <font color="#0000AA">private</font> <font color="#0000AA">static</font> <font color="#0000AA">final</font> <font color="#006699">int</font> ANZ_MA = 100;
<font color="#555555">070 </font> <font color="#0000AA">private</font> <font color="#0000AA">static</font> Mitarbeiter[] ma;
<font color="#555555">071 </font> <font color="#0000AA">private</font> <font color="#0000AA">static</font> <font color="#006699">double</font> bruttosumme;
<font color="#555555">072 </font>
<font color="#555555">073 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">074 </font> {
<font color="#555555">075 </font> ma = <font color="#0000AA">new</font> Mitarbeiter[ANZ_MA];
<font color="#555555">076 </font>
<font color="#555555">077 </font> <font color="#00AA00">//Mitarbeiter-Array füllen, z.B.</font>
<font color="#555555">078 </font> <font color="#00AA00">//ma[0] = new Manager();</font>
<font color="#555555">079 </font> <font color="#00AA00">//ma[1] = new Arbeiter();</font>
<font color="#555555">080 </font> <font color="#00AA00">//ma[2] = new Angestellter();</font>
<font color="#555555">081 </font> <font color="#00AA00">//...</font>
<font color="#555555">082 </font>
<font color="#555555">083 </font> <font color="#00AA00">//Bruttosumme berechnen</font>
<font color="#555555">084 </font> bruttosumme = 0.0;
<font color="#555555">085 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i=0; i<ma.length; ++i) {
<font color="#555555">086 </font> bruttosumme += ma[i].monatsBrutto();
<font color="#555555">087 </font> }
<font color="#555555">088 </font> System.out.println(<font color="#0000FF">"Bruttosumme = "</font>+bruttosumme);
<font color="#555555">089 </font> }
<font color="#555555">090 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Gehaltsberechnung.java"><font color="#000055" size=-1>Gehaltsberechnung.java</font></a></td>
</tr>
</table>
<i>
Listing 8.11: Abstrakte Klassen und Methoden</i></p>
<p>
Unabhängig davon, ob in einem Array-Element ein Arbeiter, Angestellter
oder Manager gespeichert wird, führt der Aufruf von <font color="#000077"><tt>monatsBrutto</tt></font>
dank der dynamischen Methodensuche die zum Typ des konkreten Objekts
passende Berechnung aus. Auch weitere Verfeinerungen der Klassenhierarchie
durch Ableiten neuer Klassen erfordern keine Veränderung in der
Routine zur Berechnung der monatlichen Bruttosumme.
<p>
So könnte beispielsweise eine neue Klasse <font color="#000077"><tt>GFManager</tt></font>
(ein Manager, der Mitglied der Geschäftsführung ist) aus
Manager abgeleitet und problemlos in die Gehaltsberechnung integriert
werden:
<a name="listingid008012"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> GFManager
<font color="#555555">002 </font><font color="#0000AA">extends</font> Manager
<font color="#555555">003 </font>{
<font color="#555555">004 </font> <font color="#006699">double</font> gfzulage;
<font color="#555555">005 </font>
<font color="#555555">006 </font> <font color="#0000AA">public</font> <font color="#006699">double</font> monatsBrutto()
<font color="#555555">007 </font> {
<font color="#555555">008 </font> <font color="#0000AA">return</font> <font color="#006699">super</font>.monatsBrutto()+gfzulage;
<font color="#555555">009 </font> }
<font color="#555555">010 </font>}</pre>
</font>
</td>
</tr>
</table>
<i>
Listing 8.12: Einfügen einer neuen Mitarbeiterklasse in die Gehaltsberechnung</i></p>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
In der abgedruckten Form ist das Programm in <a href="k100055.html#gehaltsberechnung">Listing 8.11</a>
natürlich nicht lauffähig, denn das Array <font color="#000077"><tt>ma</tt></font>
wird nicht vollständig initialisiert. Ansonsten ist es aber korrekt
und illustriert beispielhaft die Anwendung abstrakter Klassen und
Methoden.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#000077">
<tr>
<td><font color="#FFFFFF"> Hinweis </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>
<!-- Section -->
<a name="sectlevel3id008004003"></a>
<h3>8.4.3 Polymorphe Methodenaufrufe in Konstruktoren </h3>
<p>
Eine besondere Gefahrenquelle liegt darin, polymorphe Methoden im
Konstruktor einer Klasse aufzurufen. Der Grund liegt in der Initialisierungsreihenfolge
von Membervariablen während der Konstruktion eines Objekts:
<ul>
<li>Zuerst werden die Konstruktoren der Vaterklassen aufgerufen und
so deren Membervariablen initialisiert.
<li>Dann werden die Initialisierer und initialisierenden Zuweisungen
der eigenen Klasse aufgerufen.
<li>Schließlich wird der Rumpf des eigenen Konstruktors ausgeführt.
</ul>
<p>
Wird nun im eigenen Konstruktor eine Methode aufgerufen, die in einer
abgeleiteten Klasse überlagert wurde, sind die Membervariablen
der abgeleiteten Klasse noch nicht initialisiert. Ihr Konstruktor
wird ja erst später aufgerufen. Das folgende Beispiel illustriert
dieses Problem:
<a name="listingid008013"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* Listing0813.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">class</font> SingleValue
<font color="#555555">004 </font>{
<font color="#555555">005 </font> <font color="#0000AA">protected</font> <font color="#006699">int</font> value1;
<font color="#555555">006 </font>
<font color="#555555">007 </font> <font color="#0000AA">public</font> SingleValue(<font color="#006699">int</font> value1)
<font color="#555555">008 </font> {
<font color="#555555">009 </font> <font color="#006699">this</font>.value1 = value1;
<font color="#555555">010 </font> print();
<font color="#555555">011 </font> }
<font color="#555555">012 </font>
<font color="#555555">013 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> print()
<font color="#555555">014 </font> {
<font color="#555555">015 </font> System.out.println(<font color="#0000FF">"value = "</font> + value1);
<font color="#555555">016 </font> }
<font color="#555555">017 </font>}
<font color="#555555">018 </font>
<font color="#555555">019 </font><font color="#0000AA">class</font> ValuePair
<font color="#555555">020 </font><font color="#0000AA">extends</font> SingleValue
<font color="#555555">021 </font>{
<font color="#555555">022 </font> <font color="#0000AA">protected</font> <font color="#006699">int</font> value2;
<font color="#555555">023 </font>
<font color="#555555">024 </font> <font color="#0000AA">public</font> ValuePair(<font color="#006699">int</font> value1, <font color="#006699">int</font> value2)
<font color="#555555">025 </font> {
<font color="#555555">026 </font> <font color="#006699">super</font>(value1);
<font color="#555555">027 </font> <font color="#006699">this</font>.value2 = value2;
<font color="#555555">028 </font> }
<font color="#555555">029 </font>
<font color="#555555">030 </font> <font color="#0000AA">public</font> <font color="#006699">void</font> print()
<font color="#555555">031 </font> {
<font color="#555555">032 </font> System.out.println(
<font color="#555555">033 </font> <font color="#0000FF">"value = ("</font> + value1 + <font color="#0000FF">","</font> + value2 + <font color="#0000FF">")"</font>
<font color="#555555">034 </font> );
<font color="#555555">035 </font> }
<font color="#555555">036 </font>}
<font color="#555555">037 </font>
<font color="#555555">038 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing0813
<font color="#555555">039 </font>{
<font color="#555555">040 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">041 </font> {
<font color="#555555">042 </font> <font color="#0000AA">new</font> ValuePair(10,20);
<font color="#555555">043 </font> }
<font color="#555555">044 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing0813.java"><font color="#000055" size=-1>Listing0813.java</font></a></td>
</tr>
</table>
<i>
Listing 8.13: Polymorphe Methodenaufrufe im Konstruktor</i></p>
<p>
Die Ausgabe des Programms ist nicht »value = 10«, sondern
»value = (10,0)«. Bei der Konstruktion des <font color="#000077"><tt>ValuePair</tt></font>-Objekts
wird zunächst der Konstruktor der Basisklasse <font color="#000077"><tt>SingleValue</tt></font>
aufgerufen und initialisiert seine Membervariable <font color="#000077"><tt>value1</tt></font>.
Der anschließende Aufruf von <font color="#000077"><tt>print</tt></font>
wird polymorph ausgeführt, denn das zu instanzierende Objekt
ist vom Typ <font color="#000077"><tt>ValuePair</tt></font>. Da zu
diesem Zeitpunkt der Member <font color="#000077"><tt>value2</tt></font>
aber noch nicht initialisiert ist (das würde erst passieren,
wenn der Konstruktor von <font color="#000077"><tt>SingleValue</tt></font>
komplett abgearbeitet wäre), wird an seiner Stelle 0 ausgegeben
(beim Anlegen eines neuen Objekts wird der belegte Speicher mit Nullen
gefüllt). Das kann zu schwer auffindbaren Fehlern führen.
Aufrufe von Methoden, die möglicherweise überlagert werden,
sollten daher im Konstruktor vermieden werden.
<hr>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage, Addison
Wesley, Version 5.0.1
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100051.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100054.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100056.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100057.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">© 1998, 2007 Guido Krüger & Thomas
Stark, <a href="http://www.javabuch.de">http://www.javabuch.de</a>
</table>
<a name="endofbody"></a>
</body>
</html>
|