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
|
<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,k100057.html;106,k100057.html;107,k100059.html;108,k100064.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="k100057.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100057.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100059.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100064.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 9 - OOP III: Interfaces
</table>
<hr>
<!-- Section -->
<a name="interfacedefinitionen"></a>
<h2>9.1 Grundlagen <a name="ixa100546"></a><a name="ixa100547"></a>
</h2>
<hr>
<ul>
<li><a href="k100058.html#interfacedefinitionen">9.1 Grundlagen </a>
<ul>
<li><a href="k100058.html#sectlevel3id009001001">9.1.1 Definition eines Interface</a>
<li><a href="k100058.html#sectlevel3id009001002">9.1.2 Implementierung eines Interface</a>
<li><a href="k100058.html#sectlevel3id009001003">9.1.3 Verwenden eines Interface</a>
</ul>
</ul>
<hr>
<p>
Es wurde bereits erwähnt, dass es in Java keine <a name="ixa100548"><i>Mehrfachvererbung</i></a>
von Klassen gibt. Die möglichen Schwierigkeiten beim Umgang mit
mehrfacher Vererbung und die Einsicht, dass das Erben nichttrivialer
Methoden aus mehr als einer Klasse in der Praxis selten zu realisieren
ist, haben die Designer dazu veranlaßt, dieses Feature nicht
zu implementieren. Andererseits sah man es als wünschenswert
an, dass Klassen eine oder mehrere Schnittstellendefinitionen erben
können, und hat mit den <i>Interfaces</i> ein Ersatzkonstrukt
geschaffen, das dieses Feature bietet.
<!-- Section -->
<a name="sectlevel3id009001001"></a>
<h3>9.1.1 Definition eines Interface </h3>
<p>
Ein Interface ist eine besondere Form einer Klasse, die ausschließlich
abstrakte Methoden und Konstanten enthält. Anstelle des Schlüsselwortes
<a href="index_c.html#ixb100394"><font color=#000080><tt>class</tt></font></a>
wird ein Interface mit dem Bezeichner <a name="ixa100549"><a href="index_i.html#ixb100442"><font color=#000080><tt>interface</tt></font></a></a>
deklariert. Alle Methoden eines Interface sind implizit abstrakt und
öffentlich. Neben Methoden kann ein Interface auch Konstanten
enthalten, die Definition von Konstruktoren ist allerdings nicht erlaubt.
<p>
Wir wollen uns ein einfaches Beispiel ansehen. Das folgende Listing
definiert ein Interface <font color="#000077"><tt>Groesse</tt></font>,
das die drei Methoden <font color="#000077"><tt>laenge</tt></font>,
<font color="#000077"><tt>hoehe</tt></font> und <font color="#000077"><tt>breite</tt></font>
enthält:
<a name="listingid009001"></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">/* Groesse.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">public</font> <font color="#0000AA">interface</font> Groesse
<font color="#555555">004 </font>{
<font color="#555555">005 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> laenge();
<font color="#555555">006 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> hoehe();
<font color="#555555">007 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> breite();
<font color="#555555">008 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Groesse.java"><font color="#000055" size=-1>Groesse.java</font></a></td>
</tr>
</table>
<i>
Listing 9.1: Definition eines Interface</i></p>
<p>
Diese Definition ähnelt sehr einer abstrakten Klasse und dient
dazu, eine Schnittstelle für den Zugriff auf die <i>räumliche
Ausdehnung</i> eines Objekts festzulegen. Wir wollen uns an dieser
Stelle keine Gedanken über komplizierte Details wie zu verwendende
Maßeinheiten oder Objekte mit mehr oder weniger als drei Dimensionen
machen.
<!-- Section -->
<a name="sectlevel3id009001002"></a>
<h3>9.1.2 Implementierung eines Interface </h3>
<p>
Durch das bloße Definieren eines Interface wird die gewünschte
Funktionalität aber noch nicht zur Verfügung gestellt, sondern
lediglich <i>beschrieben</i>. Soll diese von einer Klasse tatsächlich
realisiert werden, muss sie das Interface <i>implementieren</i> <a name="ixa100550"></a>.
Dazu erweitert sie die <font color="#000077"><tt>class</tt></font>-Anweisung
um eine <a name="ixa100551"><a href="index_i.html#ixb100444"><font color=#000080><tt>implements</tt></font></a></a>-Klausel,
hinter der der Name des zu implementierenden Interfaces angegeben
wird. Der Compiler sorgt dafür, dass alle im Interface geforderten
Methoden definitionsgemäß implementiert werden. Zusätzlich
»verleiht« er der Klasse einen neuen Datentyp, der - wie
wir noch sehen werden - ähnliche Eigenschaften wie eine echte
Klasse hat.
<p>
Eine beispielhafte Implementierung des Interfaces <font color="#000077"><tt>Groesse</tt></font>
könnte etwa von der schon bekannten <font color="#000077"><tt>Auto</tt></font>-Klasse
vorgenommen werden:
<a name="listingauto2"></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">/* Auto2.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Auto2
<font color="#555555">004 </font><font color="#0000AA">implements</font> Groesse
<font color="#555555">005 </font>{
<font color="#555555">006 </font> <font color="#0000AA">public</font> String name;
<font color="#555555">007 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> erstzulassung;
<font color="#555555">008 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> leistung;
<font color="#555555">009 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> laenge;
<font color="#555555">010 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> hoehe;
<font color="#555555">011 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> breite;
<font color="#555555">012 </font>
<font color="#555555">013 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> laenge()
<font color="#555555">014 </font> {
<font color="#555555">015 </font> <font color="#0000AA">return</font> <font color="#006699">this</font>.laenge;
<font color="#555555">016 </font> }
<font color="#555555">017 </font>
<font color="#555555">018 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> hoehe()
<font color="#555555">019 </font> {
<font color="#555555">020 </font> <font color="#0000AA">return</font> <font color="#006699">this</font>.hoehe;
<font color="#555555">021 </font> }
<font color="#555555">022 </font>
<font color="#555555">023 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> breite()
<font color="#555555">024 </font> {
<font color="#555555">025 </font> <font color="#0000AA">return</font> <font color="#006699">this</font>.breite;
<font color="#555555">026 </font> }
<font color="#555555">027 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Auto2.java"><font color="#000055" size=-1>Auto2.java</font></a></td>
</tr>
</table>
<i>
Listing 9.2: Implementierung eines Interface</i></p>
<p>
Wir haben die Klasse dazu um drei veränderliche Instanzmerkmale
erweitert, die es uns erlauben, die vom Interface geforderten Methoden
auf einfache Weise zu implementieren. Ebenso wie die Klasse <font color="#000077"><tt>Auto</tt></font>
könnte auch jede andere Klasse das Interface <font color="#000077"><tt></tt></font>
implementieren und so Informationen über seine räumliche
Ausdehnung geben:
<a name="listingid009003"></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> FussballPlatz
<font color="#555555">002 </font><font color="#0000AA">implements</font> Groesse
<font color="#555555">003 </font>{
<font color="#555555">004 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> laenge()
<font color="#555555">005 </font> {
<font color="#555555">006 </font> <font color="#0000AA">return</font> 105000;
<font color="#555555">007 </font> }
<font color="#555555">008 </font>
<font color="#555555">009 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> hoehe()
<font color="#555555">010 </font> {
<font color="#555555">011 </font> <font color="#0000AA">return</font> 0;
<font color="#555555">012 </font> }
<font color="#555555">013 </font>
<font color="#555555">014 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> breite()
<font color="#555555">015 </font> {
<font color="#555555">016 </font> <font color="#0000AA">return</font> 70000;
<font color="#555555">017 </font> }
<font color="#555555">018 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/FussballPlatz.java"><font color="#000055" size=-1>FussballPlatz.java</font></a></td>
</tr>
</table>
<i>
Listing 9.3: Die Klasse FussballPlatz</i></p>
<p>
Hier geben die Interface-Methoden konstante Werte zurück (in
der Annahme, dass alle Fußballplätze gleich groß
sind). Ebenso gut ist es möglich, dass die Größe von
anderen Instanzmerkmalen abhängig ist und zur Implementierung
des Interfaces etwas mehr Aufwand getrieben werden muss:
<a name="listingid009004"></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">/* PapierBlatt.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> PapierBlatt
<font color="#555555">004 </font><font color="#0000AA">implements</font> Groesse
<font color="#555555">005 </font>{
<font color="#555555">006 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> format; <font color="#00AA00">//0=DIN A0, 1=DIN A1 usw.</font>
<font color="#555555">007 </font>
<font color="#555555">008 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> laenge()
<font color="#555555">009 </font> {
<font color="#555555">010 </font> <font color="#006699">int</font> ret = 0;
<font color="#555555">011 </font> <font color="#0000AA">if</font> (format == 0) {
<font color="#555555">012 </font> ret = 1189;
<font color="#555555">013 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (format == 1) {
<font color="#555555">014 </font> ret = 841;
<font color="#555555">015 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (format == 2) {
<font color="#555555">016 </font> ret = 594;
<font color="#555555">017 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (format == 3) {
<font color="#555555">018 </font> ret = 420;
<font color="#555555">019 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (format == 4) {
<font color="#555555">020 </font> ret = 297;
<font color="#555555">021 </font> }
<font color="#555555">022 </font> <font color="#00AA00">//usw...</font>
<font color="#555555">023 </font> <font color="#0000AA">return</font> ret;
<font color="#555555">024 </font> }
<font color="#555555">025 </font>
<font color="#555555">026 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> hoehe()
<font color="#555555">027 </font> {
<font color="#555555">028 </font> <font color="#0000AA">return</font> 0;
<font color="#555555">029 </font> }
<font color="#555555">030 </font>
<font color="#555555">031 </font> <font color="#0000AA">public</font> <font color="#006699">int</font> breite()
<font color="#555555">032 </font> {
<font color="#555555">033 </font> <font color="#006699">int</font> ret = 0;
<font color="#555555">034 </font> <font color="#0000AA">if</font> (format == 0) {
<font color="#555555">035 </font> ret = 841;
<font color="#555555">036 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (format == 1) {
<font color="#555555">037 </font> ret = 594;
<font color="#555555">038 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (format == 2) {
<font color="#555555">039 </font> ret = 420;
<font color="#555555">040 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (format == 3) {
<font color="#555555">041 </font> ret = 297;
<font color="#555555">042 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (format == 4) {
<font color="#555555">043 </font> ret = 210;
<font color="#555555">044 </font> }
<font color="#555555">045 </font> <font color="#00AA00">//usw...</font>
<font color="#555555">046 </font> <font color="#0000AA">return</font> ret;
<font color="#555555">047 </font> }
<font color="#555555">048 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/PapierBlatt.java"><font color="#000055" size=-1>PapierBlatt.java</font></a></td>
</tr>
</table>
<i>
Listing 9.4: Die Klasse PapierBlatt</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>
Die Art der Realisierung der vereinbarten Methoden spielt für
das Implementieren eines Interface keine Rolle. Tatsächlich kommt
es ausgesprochen häufig vor, dass Interfaces von sehr unterschiedlichen
Klassen implementiert und die erforderlichen Methoden auf sehr unterschiedliche
Weise realisiert werden.</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>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#0099CC"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=1></td>
<td width=1 align=left valign=top bgcolor="#0099CC"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Eine Klasse kann ein Interface auch dann implementieren, wenn sie
nicht alle seine Methoden implementiert. In diesem Fall ist die Klasse
allerdings als <a name="ixa100552"><a href="index_a.html#ixb100440"><font color=#000080><tt>abstract</tt></font></a></a>
zu deklarieren und kann nicht dazu verwendet werden, Objekte zu instanzieren.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#0099CC">
<tr>
<td><font color="#FFFFFF"> Tipp </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#0099CC"><img src="trp1_1.gif"></td>
</tr>
</table>
<!-- Section -->
<a name="sectlevel3id009001003"></a>
<h3>9.1.3 Verwenden eines Interface </h3>
<p>
Nützlich ist ein Interface immer dann, wenn <i>Eigenschaften</i>
einer Klasse beschrieben werden sollen, die nicht direkt in seiner
normalen Vererbungshierarchie abgebildet werden können. Hätten
wir beispielsweise <font color="#000077"><tt>Groesse</tt></font> als
abstrakte Klasse definiert, ergäbe sich eine sehr unnatürliche
Ableitungshierarchie, wenn Autos, Fußballplätze und Papierblätter
daraus abgeleitet wären. Durch Implementieren des <font color="#000077"><tt>Groesse</tt></font>-Interfaces
können sie die Verfügbarkeit der drei Methoden <font color="#000077"><tt>laenge</tt></font>,
<font color="#000077"><tt>hoehe</tt></font> und <font color="#000077"><tt>breite</tt></font>
dagegen <i>unabhängig</i> von ihrer eigenen Vererbungslinie garantieren.
<p>
Wir wollen uns ein einfaches Beispiel für die Anwendung des Interfaces
ansehen. Dazu soll eine Methode <font color="#000077"><tt>grundflaeche</tt></font>
entwickelt werden, die zu jedem Objekt, das das Interface <font color="#000077"><tt>Groesse</tt></font>
implementiert, dessen Grundfläche (Länge mal Breite) berechnet:
<a name="grundflaeche"></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">/* Listing0905.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing0905
<font color="#555555">004 </font>{
<font color="#555555">005 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">long</font> grundflaeche(Groesse g)
<font color="#555555">006 </font> {
<font color="#555555">007 </font> <font color="#0000AA">return</font> (<font color="#006699">long</font>)g.laenge() * g.breite();
<font color="#555555">008 </font> }
<font color="#555555">009 </font>
<font color="#555555">010 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">011 </font> {
<font color="#555555">012 </font> <font color="#00AA00">//Zuerst erzeugen wir ein Auto2...</font>
<font color="#555555">013 </font> Auto2 auto = <font color="#0000AA">new</font> Auto2();
<font color="#555555">014 </font> auto.laenge = 4235;
<font color="#555555">015 </font> auto.hoehe = 1650;
<font color="#555555">016 </font> auto.breite = 1820;
<font color="#555555">017 </font> <font color="#00AA00">//Nun ein DIN A4-Blatt...</font>
<font color="#555555">018 </font> PapierBlatt blatt = <font color="#0000AA">new</font> PapierBlatt();
<font color="#555555">019 </font> blatt.format = 4;
<font color="#555555">020 </font> <font color="#00AA00">//Und zum Schluß einen Fußballplatz...</font>
<font color="#555555">021 </font> FussballPlatz platz = <font color="#0000AA">new</font> FussballPlatz();
<font color="#555555">022 </font> <font color="#00AA00">//Nun werden sie ausgegeben</font>
<font color="#555555">023 </font> System.out.println(<font color="#0000FF">"Auto: "</font> + grundflaeche(auto));
<font color="#555555">024 </font> System.out.println(<font color="#0000FF">"Blatt: "</font> + grundflaeche(blatt));
<font color="#555555">025 </font> System.out.println(<font color="#0000FF">"Platz: "</font> + grundflaeche(platz));
<font color="#555555">026 </font> }
<font color="#555555">027 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing0905.java"><font color="#000055" size=-1>Listing0905.java</font></a></td>
</tr>
</table>
<i>
Listing 9.5: Verwendung eines Interface</i></p>
<p>
Das Programm erzeugt zunächst einige Objekte, die das <font color="#000077"><tt>Groesse</tt></font>-Interface
implementieren. Anschließend werden sie an die Methode <font color="#000077"><tt>grundflaeche</tt></font>
übergeben, deren Argument <font color="#000077"><tt>g</tt></font>
vom Typ <font color="#000077"><tt>Groesse</tt></font> ist. Durch diese
Typisierung kann der Compiler sicherstellen, dass nur Objekte »des
Typs« <font color="#000077"><tt>Groesse</tt></font> an <font color="#000077"><tt>grundflaeche</tt></font>
übergeben werden. Das ist genau dann der Fall, wenn das übergebene
Objekt dieses Interface implementiert.
<p>
Die Ausgabe des Programms ist:
<font color="#333300">
<pre>
Auto: 7707700
Blatt: 62370
Platz: 7350000000
</pre>
</font>
<p>
An diesem Beispiel kann man bereits die wichtigste Gemeinsamkeit zwischen
abstrakten Klassen und Interfaces erkennen: Beide können im Programm
zur Deklaration von lokalen Variablen, Membervariablen oder Methodenparametern
verwendet werden. Eine Interface-Variable ist kompatibel zu allen
Objekten, deren Klassen dieses Interface implementieren.
<p>
Auch der <a href="index_i.html#ixb100330"><font color=#000080><tt>instanceof</tt></font></a>-Operator
kann auf Interfacenamen angewendet werden. Eine alternative Implementierung
der Methode <font color="#000077"><tt>grundflaeche</tt></font>, die
mit allen Objekttypen funktioniert, könnte dann etwa so aussehen:
<a name="listingid009006"></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">static</font> <font color="#006699">long</font> grundflaeche(Object o)
<font color="#555555">002 </font>{
<font color="#555555">003 </font> <font color="#006699">long</font> ret = 0;
<font color="#555555">004 </font> <font color="#0000AA">if</font> (o <font color="#0000AA">instanceof</font> Groesse) {
<font color="#555555">005 </font> Groesse g = (Groesse)o;
<font color="#555555">006 </font> ret = (<font color="#006699">long</font>)g.laenge() * g.breite();
<font color="#555555">007 </font> }
<font color="#555555">008 </font> <font color="#0000AA">return</font> ret;
<font color="#555555">009 </font>}</pre>
</font>
</td>
</tr>
</table>
<i>
Listing 9.6: Der instanceof-Operator auf Interfaces</i></p>
<p>
Diese Implementierung verhält sich für alle Objekte, die
das Interface <font color="#000077"><tt>Groesse</tt></font> implementieren,
so wie die vorige. Für alle übrigen Objekte wird 0 zurückgegeben.
<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="k100057.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100057.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100059.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100064.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>
|