-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterfaces.html
1549 lines (1427 loc) · 168 KB
/
interfaces.html
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en" data-theme="light">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.18.1: http://docutils.sourceforge.net/" />
<title>Module 8, Part 2: Java interfaces — 02312 Introductory Programming — Fall 2024</title>
<script data-cfasync="false">
document.documentElement.dataset.mode = localStorage.getItem("mode") || "light";
document.documentElement.dataset.theme = localStorage.getItem("theme") || "light";
</script>
<!-- Loaded before other Sphinx assets -->
<link href="_static/styles/theme.css?digest=e353d410970836974a52" rel="stylesheet" />
<link href="_static/styles/bootstrap.css?digest=e353d410970836974a52" rel="stylesheet" />
<link href="_static/styles/pydata-sphinx-theme.css?digest=e353d410970836974a52" rel="stylesheet" />
<link href="_static/vendor/fontawesome/6.1.2/css/all.min.css?digest=e353d410970836974a52" rel="stylesheet" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.1.2/webfonts/fa-solid-900.woff2" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.1.2/webfonts/fa-brands-400.woff2" />
<link rel="preload" as="font" type="font/woff2" crossorigin href="_static/vendor/fontawesome/6.1.2/webfonts/fa-regular-400.woff2" />
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
<link rel="stylesheet" href="_static/styles/sphinx-book-theme.css?digest=14f4ca6b54d191a8c7657f6c759bf11a5fb86285" type="text/css" />
<link rel="stylesheet" type="text/css" href="_static/exercise.css" />
<link rel="stylesheet" type="text/css" href="_static/proof.css" />
<link rel="stylesheet" type="text/css" href="_static/copybutton.css" />
<link rel="stylesheet" type="text/css" href="_static/tabs.css" />
<link rel="stylesheet" type="text/css" href="_static/custom.css" />
<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="_static/scripts/bootstrap.js?digest=e353d410970836974a52" />
<link rel="preload" as="script" href="_static/scripts/pydata-sphinx-theme.js?digest=e353d410970836974a52" />
<script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
<script src="_static/doctools.js"></script>
<script src="_static/sphinx_highlight.js"></script>
<script src="_static/clipboard.min.js"></script>
<script src="_static/copybutton.js"></script>
<script src="_static/scripts/sphinx-book-theme.js?digest=5a5c038af52cf7bc1a1ec88eea08e6366ee68824"></script>
<script src="_static/proof/proof.js"></script>
<link rel="icon" href="_static/favicon.ico"/>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Module 9, Part 1: Lab Day" href="lab-day-4.html" />
<link rel="prev" title="Module 8, Part 1: Programming Interfaces and Encapsulation" href="prog-interf-encapsulation.html" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="docsearch:language" content="en"/>
</head>
<body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="light">
<a class="skip-link" href="#main-content">Skip to main content</a>
<input type="checkbox"
class="sidebar-toggle"
name="__primary"
id="__primary"/>
<label class="overlay overlay-primary" for="__primary"></label>
<input type="checkbox"
class="sidebar-toggle"
name="__secondary"
id="__secondary"/>
<label class="overlay overlay-secondary" for="__secondary"></label>
<div class="search-button__wrapper">
<div class="search-button__overlay"></div>
<div class="search-button__search-container">
<form class="bd-search d-flex align-items-center"
action="search.html"
method="get">
<i class="fa-solid fa-magnifying-glass"></i>
<input type="search"
class="form-control"
name="q"
id="search-input"
placeholder="Search..."
aria-label="Search..."
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"/>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span>
</form></div>
</div>
<nav class="bd-header navbar navbar-expand-lg bd-navbar">
</nav>
<div class="bd-container">
<div class="bd-container__inner bd-page-width">
<div class="bd-sidebar-primary bd-sidebar">
<div class="sidebar-header-items sidebar-primary__section">
</div>
<div class="sidebar-primary-items__start sidebar-primary__section">
<div class="sidebar-primary-item">
<a class="navbar-brand logo" href="index.html">
<img src="_static/dtu.png" class="logo__image only-light" alt="Logo image"/>
<script>document.write(`<img src="_static/dtu.png" class="logo__image only-dark" alt="Logo image"/>`);</script>
</a></div>
<div class="sidebar-primary-item"><nav class="bd-links" id="bd-docs-nav" aria-label="Main">
<div class="bd-toc-item navbar-nav active">
<p aria-level="2" class="caption" role="heading"><span class="caption-text">Contents</span></p>
<ul class="current nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="overview.html">Module 0: Overview of the Course and Assessment</a></li>
<li class="toctree-l1"><a class="reference internal" href="basic-programming.html">Module 1: Basic Notions of Computing and Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="java-first-taste.html">Module 2, Part 1: a First Taste of Java</a></li>
<li class="toctree-l1"><a class="reference internal" href="io-conditionals-strings.html">Module 2, Part 2: Console I/O, Conditionals, Strings</a></li>
<li class="toctree-l1"><a class="reference internal" href="lab-day-1.html">Module 3, Part 1: Lab Day</a></li>
<li class="toctree-l1"><a class="reference internal" href="loops.html">Module 3, Part 2: Loops</a></li>
<li class="toctree-l1"><a class="reference internal" href="loops2.html">Module 4, Part 1: More About Loops</a></li>
<li class="toctree-l1"><a class="reference internal" href="structured.html">Module 4, Part 2: Structured Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="arrays.html">Module 5, Part 1: Arrays</a></li>
<li class="toctree-l1"><a class="reference internal" href="lab-day-2.html">Module 5, Part 2: Lab Day</a></li>
<li class="toctree-l1"><a class="reference internal" href="simple-classes.html">Module 6, Part 1: Simple Classes and Objects</a></li>
<li class="toctree-l1"><a class="reference internal" href="arrays-simple-classes-2.html">Module 6, Part 2: More About Arrays and Objects</a></li>
<li class="toctree-l1"><a class="reference internal" href="lab-day-3.html">Module 7, Part 1: Lab Day</a></li>
<li class="toctree-l1"><a class="reference internal" href="references-null.html">Module 7, Part 2: References, <code class="docutils literal notranslate"><span class="pre">null</span></code> values, and the <code class="docutils literal notranslate"><span class="pre">NullPointerException</span></code></a></li>
<li class="toctree-l1"><a class="reference internal" href="prog-interf-encapsulation.html">Module 8, Part 1: Programming Interfaces and Encapsulation</a></li>
<li class="toctree-l1 current active"><a class="current reference internal" href="#">Module 8, Part 2: Java <code class="docutils literal notranslate"><span class="pre">interface</span></code>s</a></li>
<li class="toctree-l1"><a class="reference internal" href="lab-day-4.html">Module 9, Part 1: Lab Day</a></li>
<li class="toctree-l1"><a class="reference internal" href="inheritance-oo.html">Module 9, Part 2: Class Inheritance and Principles of Object-Oriented Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="inheritance-2.html">Module 10, part 1: More on Class Inheritance and <code class="docutils literal notranslate"><span class="pre">abstract</span> <span class="pre">class</span></code>es</a></li>
<li class="toctree-l1"><a class="reference internal" href="classes-polymorphism.html">Module 10, Part 2: More on Java Classes and Polymorphism</a></li>
<li class="toctree-l1"><a class="reference internal" href="lab-day-5.html">Module 11, Part 1: Lab Day</a></li>
<li class="toctree-l1"><a class="reference internal" href="errors.html">Module 11, Part 2: Error Handling with Exceptions</a></li>
<li class="toctree-l1"><a class="reference internal" href="files.html">Module 12, Part 1: File I/O</a></li>
<li class="toctree-l1"><a class="reference internal" href="projects.html">Module 12, Part 2: Managing Java Projects: Packages, JAR Files, Build Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="lab-day-6.html">Module 13, Part 1: Lab Day — Exam Simulation</a></li>
<li class="toctree-l1"><a class="reference internal" href="exam-qa-lab.html">Module 13, Part 2: Exam Q&A, Review, and Final Lab</a></li>
<li class="toctree-l1"><a class="reference internal" href="changelog.html">ChangeLog</a></li>
<li class="toctree-l1"><a class="reference internal" href="jgrader.html">Appendix: Information About JGrader</a></li>
</ul>
</div>
</nav></div>
</div>
<div class="sidebar-primary-items__end sidebar-primary__section">
<div class="sidebar-primary-item"><nav class="sidebar-indices-items">
<p class="sidebar-indices-items__title" role="heading" aria-level="1">Indices</p>
<ul class="indices-link">
<li class="toctree-l1">
<a class="reference internal"
href="genindex.html"
accesskey="I">General Index</a>
</li>
</ul>
</nav></div>
</div>
<div id="rtd-footer-container"></div>
</div>
<main id="main-content" class="bd-main">
<div class="sbt-scroll-pixel-helper"></div>
<div class="bd-content">
<div class="bd-article-container">
<div class="bd-header-article">
<div class="header-article-items header-article__inner">
<div class="header-article-items__start">
<div class="header-article-item"><label class="sidebar-toggle primary-toggle btn btn-sm" for="__primary" title="Toggle primary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="fa-solid fa-bars"></span>
</label></div>
</div>
<div class="header-article-items__end">
<div class="header-article-item">
<div class="article-header-buttons">
<button onclick="toggleFullScreen()"
class="btn btn-sm btn-fullscreen-button"
title="Fullscreen mode"
data-bs-placement="bottom" data-bs-toggle="tooltip"
>
<span class="btn__icon-container">
<i class="fas fa-expand"></i>
</span>
</button>
<script>
document.write(`
<button class="btn btn-sm navbar-btn search-button search-button__button" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
</button>
`);
</script>
<label class="sidebar-toggle secondary-toggle btn btn-sm" for="__secondary"title="Toggle secondary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="fa-solid fa-list"></span>
</label>
</div></div>
</div>
</div>
</div>
<div id="jb-print-docs-body" class="onlyprint">
<h1>Module 8, Part 2: Java interfaces</h1>
<!-- Table of contents -->
<div id="print-main-content">
<div id="jb-print-toc">
<div>
<h2> Contents </h2>
</div>
<nav aria-label="Page">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#defining-and-using-interfaces-in-java">Defining and Using <code class="docutils literal notranslate"><span class="pre">interface</span></code>s in Java</a><ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#the-problem-the-dtu-christmas-party-management-software">The Problem: the DTU Christmas Party Management Software</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#implementing-the-dtu-christmas-party-software-using-interfaces">Implementing the DTU Christmas Party Software Using <code class="docutils literal notranslate"><span class="pre">interface</span></code>s</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#extending-interfaces-and-implementing-multiple-interfaces">Extending Interfaces and Implementing Multiple Interfaces</a></li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#concluding-remarks">Concluding Remarks</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#references-and-further-readings">References and Further Readings</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#exercises">Exercises</a></li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#lab-and-weekly-assessments">Lab and Weekly Assessments</a><ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#vehicle-fleet-management">04 - Vehicle Fleet Management</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#electronic-devices-shop">05 - Electronic Devices Shop</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#video-game-monsters-part-2">06 - Video Game Monsters, Part 2</a></li>
</ul>
</li>
</ul>
</nav>
</div>
</div>
</div>
<div id="searchbox"></div>
<article class="bd-article" role="main">
<section class="tex2jax_ignore mathjax_ignore" id="module-8-part-2-java-interfaces">
<span id="mod-interfaces"></span><h1>Module 8, Part 2: Java <code class="docutils literal notranslate"><span class="pre">interface</span></code>s<a class="headerlink" href="#module-8-part-2-java-interfaces" title="Permalink to this heading">#</a></h1>
<p>In this second part of Module 8 we study how to
<a class="reference internal" href="#sec-interfaces-interfaces"><span class="std std-ref">define a Java interface</span></a>, and how to write
reusable code based on that interface.</p>
<span class="target" id="index-0"></span><section id="defining-and-using-interfaces-in-java">
<span id="sec-interfaces-interfaces"></span><span id="index-1"></span><h2>Defining and Using <code class="docutils literal notranslate"><span class="pre">interface</span></code>s in Java<a class="headerlink" href="#defining-and-using-interfaces-in-java" title="Permalink to this heading">#</a></h2>
<p>When designing and writing Java programs, we may sometimes need to handle
objects that should intuitively belong to different classes, and yet, have very
similar <a class="reference internal" href="prog-interf-encapsulation.html#sec-interfaces-encapsulation-interfaces-general"><span class="std std-ref">programming interfaces</span></a>.</p>
<p>In these situations, Java allows us to define and give a name to commonly-used
programming interfaces, by using the keyword <code class="docutils literal notranslate"><span class="pre">interface</span></code>; then, we can write
reusable code that works on any object that provides the desired interface.</p>
<p>The idea, in a nutshell, is:</p>
<ul>
<li><p>We can define an interface with a list of so-called <strong>abstract methods</strong>, i.e.
method names with argument and return types, but <em>without a body</em>. For
example, we may define an interface like the following: intuitively, it
describes any object that “can be moved,” and contains one abstract method
called <code class="docutils literal notranslate"><span class="pre">move</span></code> that takes two <code class="docutils literal notranslate"><span class="pre">double</span></code>s and returns nothing (<code class="docutils literal notranslate"><span class="pre">void</span></code>):</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">interface</span> <span class="nc">CanBeMoved</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">move</span><span class="p">(</span><span class="kt">double</span><span class="w"> </span><span class="n">dx</span><span class="p">,</span><span class="w"> </span><span class="kt">double</span><span class="w"> </span><span class="n">dy</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
</li>
<li><p>Then, we define our classes as usual — but when defining a class, we can
also specify that the class <strong>implements</strong> one or more <code class="docutils literal notranslate"><span class="pre">interface</span></code>s — i.e.
the class provides all the methods required by the interface. For example,
reprising the class <code class="docutils literal notranslate"><span class="pre">Point</span></code> from
<a class="reference internal" href="simple-classes.html#lab-simple-classes-point"><span class="std std-ref">a previous assignment</span></a>, we can write:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="linenos">1</span><span class="kd">class</span> <span class="nc">Point</span><span class="w"> </span><span class="kd">implements</span><span class="w"> </span><span class="n">CanBeMoved</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">2</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">double</span><span class="w"> </span><span class="n">x</span><span class="p">;</span>
<span class="linenos">3</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">double</span><span class="w"> </span><span class="n">y</span><span class="p">;</span>
<span class="linenos">4</span>
<span class="linenos">5</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">move</span><span class="p">(</span><span class="kt">double</span><span class="w"> </span><span class="n">dx</span><span class="p">,</span><span class="w"> </span><span class="kt">double</span><span class="w"> </span><span class="n">dy</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">6</span><span class="w"> </span><span class="c1">// ...code that adds 'dx' and 'dy' to the point coordinates...</span>
<span class="linenos">7</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">8</span><span class="p">}</span>
</pre></div>
</div>
<p>On line 1, <code class="docutils literal notranslate"><span class="pre">...</span> <span class="pre">implements</span> <span class="pre">CanBeMoved</span> <span class="pre">...</span></code> means that the class <code class="docutils literal notranslate"><span class="pre">Point</span></code>
satisfies the requirements of the interface <code class="docutils literal notranslate"><span class="pre">CanBeMoved</span></code> — i.e., the class
includes the definition of the method <code class="docutils literal notranslate"><span class="pre">public</span> <span class="pre">void</span> <span class="pre">move(double</span> <span class="pre">dx,</span> <span class="pre">double</span> <span class="pre">dy)</span></code>
(including the method body, i.e. the actual <em>implementation</em> of the method).
Note that, if the class <code class="docutils literal notranslate"><span class="pre">Point</span></code> does <em>not</em> implement all the method(s)
required by the interface <code class="docutils literal notranslate"><span class="pre">CanBeMoved</span></code>, Java will report an error.</p>
</li>
<li><p>As a consequence, we will be able to call all the methods of the interface
<code class="docutils literal notranslate"><span class="pre">CanBeMoved</span></code> on any object of the class <code class="docutils literal notranslate"><span class="pre">Point</span></code> — e.g. if <code class="docutils literal notranslate"><span class="pre">p</span></code> has type
<code class="docutils literal notranslate"><span class="pre">Point</span></code>, then we can call <code class="docutils literal notranslate"><span class="pre">p.move(...)</span></code>. Moreover, every object of the class
<code class="docutils literal notranslate"><span class="pre">Point</span></code> also gets the additional type <code class="docutils literal notranslate"><span class="pre">CanBeMoved</span></code>.</p></li>
</ul>
<p>To explore more concretely how Java <code class="docutils literal notranslate"><span class="pre">interface</span></code>s work and when they are useful,
we start with a problem: writing a program for
<a class="reference internal" href="#sec-interfaces-christmas"><span class="std std-ref">managing the DTU Christmas party</span></a>.
Then, we discuss:</p>
<ul class="simple">
<li><p>a <a class="reference internal" href="#eg-dtu-christmas-party-two-classes-no-interfaces">first solution</a>
based on using classes and objects as we have seen thus far;</p></li>
<li><p>a <a class="reference internal" href="#eg-dtu-christmas-party-one-class-no-interfaces">second solution</a>,
also based on using classes and objects as we have seen thus far;</p></li>
<li><p>a <a class="reference internal" href="#sec-interfaces-christmas-solution"><span class="std std-ref">third solution</span></a>
that addresses the drawbacks of the previous two solutions,
by defining and using an <code class="docutils literal notranslate"><span class="pre">interface</span></code>.</p></li>
</ul>
<section id="the-problem-the-dtu-christmas-party-management-software">
<span id="sec-interfaces-christmas"></span><h3>The Problem: the DTU Christmas Party Management Software<a class="headerlink" href="#the-problem-the-dtu-christmas-party-management-software" title="Permalink to this heading">#</a></h3>
<p>Consider this scenario: DTU is organising its annual Christmas party, and asks
us to write a program to handle the list of people who will attend. The
requirements are the following:</p>
<ul class="simple">
<li><p>All DTU employees can attend the party;</p></li>
<li><p>Guests with a ticket can also attend the party;</p></li>
<li><p>The program must provide a description of each person attending the party
— including:</p>
<ul>
<li><p>the person’s role (employee or guest), and</p></li>
<li><p>the person’s name, and</p></li>
<li><p>either:</p>
<ul>
<li><p>the department (if the person is an employee), or</p></li>
<li><p>the ticket number (if the person is a guest).</p></li>
</ul>
</li>
</ul>
</li>
<li><p>The program must print three different reports showing:</p>
<ul>
<li><p>the list of DTU employees attending the party;</p></li>
<li><p>the list of guests attending the party;</p></li>
<li><p>the list of all people attending the party.</p></li>
</ul>
</li>
</ul>
<p>The requirements above suggests that we should have some class to represent and
store the information of each individual employee or guest at the party.</p>
<p>The examples below address this problem in two ways:</p>
<ul class="simple">
<li><p>using <a class="reference internal" href="#eg-dtu-christmas-party-two-classes-no-interfaces">separate classes for representing an employee and a guest</a>,
and</p></li>
<li><p>using <a class="reference internal" href="#eg-dtu-christmas-party-one-class-no-interfaces">one class "Person" for representing both an employee and a guest</a>.</p></li>
</ul>
<div class="proof example admonition" id="eg-dtu-christmas-party-two-classes-no-interfaces">
<p class="admonition-title"><span class="caption-number">Example 51 </span> (DTU Christmas Party — solution 1 (<code class="docutils literal notranslate"><span class="pre">Employee</span></code> and <code class="docutils literal notranslate"><span class="pre">Guest</span></code> classes))</p>
<section class="example-content" id="proof-content">
<p>We could address the task by defining two classes for the two kinds of people
that can attend the Christmas party: one class representing a <code class="docutils literal notranslate"><span class="pre">Guest</span></code> and
another class representing a DTU <code class="docutils literal notranslate"><span class="pre">Employee</span></code>, as shown below. Both classes have
a field to store the person’s <code class="docutils literal notranslate"><span class="pre">name</span></code> (as a <code class="docutils literal notranslate"><span class="pre">String</span></code>) and provide a method
<code class="docutils literal notranslate"><span class="pre">getInfo()</span></code> that returns a <code class="docutils literal notranslate"><span class="pre">String</span></code> describing the person.</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="linenos"> 1</span><span class="kd">class</span> <span class="nc">Employee</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 2</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">name</span><span class="p">;</span>
<span class="linenos"> 3</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">department</span><span class="p">;</span>
<span class="linenos"> 4</span>
<span class="linenos"> 5</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="nf">Employee</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">name</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">department</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 6</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">name</span><span class="p">;</span>
<span class="linenos"> 7</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">department</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">department</span><span class="p">;</span>
<span class="linenos"> 8</span><span class="w"> </span><span class="p">}</span>
<span class="linenos"> 9</span>
<span class="linenos">10</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getInfo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">11</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="s">"Employee, "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">department</span><span class="p">;</span>
<span class="linenos">12</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">13</span><span class="p">}</span>
<span class="linenos">14</span>
<span class="linenos">15</span><span class="kd">class</span> <span class="nc">Guest</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">16</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">name</span><span class="p">;</span>
<span class="linenos">17</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">ticketNumber</span><span class="p">;</span>
<span class="linenos">18</span>
<span class="linenos">19</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="nf">Guest</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">name</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">ticketNumber</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">20</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">name</span><span class="p">;</span>
<span class="linenos">21</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">ticketNumber</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ticketNumber</span><span class="p">;</span>
<span class="linenos">22</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">23</span>
<span class="linenos">24</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getInfo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">25</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="s">"Guest, ticket "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">ticketNumber</span><span class="p">;</span>
<span class="linenos">26</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">27</span><span class="p">}</span>
</pre></div>
</div>
<p>Using the classes above, we can write the following sample program, that creates
a few employees and guests, stores them into arrays, and prints a draft of the
reports outlined in the software requirements above.</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="linenos"> 1</span><span class="kd">class</span> <span class="nc">ChristmasParty</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 2</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span><span class="w"> </span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 3</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">e1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Employee</span><span class="p">(</span><span class="s">"Ida Iversen"</span><span class="p">,</span><span class="w"> </span><span class="s">"DTU Aqua"</span><span class="p">);</span>
<span class="linenos"> 4</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">e2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Employee</span><span class="p">(</span><span class="s">"Oliver Olesen"</span><span class="p">,</span><span class="w"> </span><span class="s">"DTU Construct"</span><span class="p">);</span>
<span class="linenos"> 5</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">e3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Employee</span><span class="p">(</span><span class="s">"Alceste Scalas"</span><span class="p">,</span><span class="w"> </span><span class="s">"DTU Compute"</span><span class="p">);</span>
<span class="linenos"> 6</span>
<span class="linenos"> 7</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">g1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Guest</span><span class="p">(</span><span class="s">"Laura Lund"</span><span class="p">,</span><span class="w"> </span><span class="mi">123</span><span class="p">);</span>
<span class="linenos"> 8</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">g2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Guest</span><span class="p">(</span><span class="s">"Emma Ebbesen"</span><span class="p">,</span><span class="w"> </span><span class="mi">75</span><span class="p">);</span>
<span class="linenos"> 9</span>
<span class="linenos">10</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">employees</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Employee</span><span class="o">[]</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">e1</span><span class="p">,</span><span class="w"> </span><span class="n">e2</span><span class="p">,</span><span class="w"> </span><span class="n">e3</span><span class="w"> </span><span class="p">};</span>
<span class="linenos">11</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">guests</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Guest</span><span class="o">[]</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">g1</span><span class="p">,</span><span class="w"> </span><span class="n">g2</span><span class="w"> </span><span class="p">};</span>
<span class="linenos">12</span>
<span class="linenos">13</span><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"List of people at the DTU Christmas party: "</span><span class="p">);</span>
<span class="linenos">14</span><span class="w"> </span><span class="n">printEmployees</span><span class="p">(</span><span class="n">employees</span><span class="p">);</span>
<span class="linenos">15</span><span class="w"> </span><span class="n">printGuests</span><span class="p">(</span><span class="n">guests</span><span class="p">);</span>
<span class="linenos">16</span>
<span class="linenos">17</span><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"List of DTU employees at the party: "</span><span class="p">);</span>
<span class="linenos">18</span><span class="w"> </span><span class="n">printEmployees</span><span class="p">(</span><span class="n">employees</span><span class="p">);</span>
<span class="linenos">19</span>
<span class="linenos">20</span><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"List of guests at the party: "</span><span class="p">);</span>
<span class="linenos">21</span><span class="w"> </span><span class="n">printGuests</span><span class="p">(</span><span class="n">guests</span><span class="p">);</span>
<span class="linenos">22</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">23</span>
<span class="linenos">24</span><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">printEmployees</span><span class="p">(</span><span class="n">Employee</span><span class="o">[]</span><span class="w"> </span><span class="n">employees</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">25</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">var</span><span class="w"> </span><span class="n">e</span><span class="p">:</span><span class="w"> </span><span class="n">employees</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">26</span><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">" - "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="na">name</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">" ("</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="na">getInfo</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">")"</span><span class="p">);</span>
<span class="linenos">27</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">28</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">29</span>
<span class="linenos">30</span><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">printGuests</span><span class="p">(</span><span class="n">Guest</span><span class="o">[]</span><span class="w"> </span><span class="n">guests</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">31</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">var</span><span class="w"> </span><span class="n">g</span><span class="p">:</span><span class="w"> </span><span class="n">guests</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">32</span><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">" - "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">g</span><span class="p">.</span><span class="na">name</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">" ("</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">g</span><span class="p">.</span><span class="na">getInfo</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">")"</span><span class="p">);</span>
<span class="linenos">33</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">34</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">35</span><span class="p">}</span>
</pre></div>
</div>
<p>The program’s output satisfies the requirements:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>List of people at the DTU Christmas party:
- Ida Iversen (Employee, DTU Aqua)
- Oliver Olesen (Employee, DTU Construct)
- Alceste Scalas (Employee, DTU Compute)
- Laura Lund (Guest, ticket 123)
- Emma Ebbesen (Guest, ticket 75)
List of DTU employees at the party:
- Ida Iversen (Employee, DTU Aqua)
- Oliver Olesen (Employee, DTU Construct)
- Alceste Scalas (Employee, DTU Compute)
List of guests at the party:
- Laura Lund (Guest, ticket 123)
- Emma Ebbesen (Guest, ticket 75)
</pre></div>
</div>
<p>However, we can notice two drawbacks:</p>
<ul class="simple">
<li><p>there is <strong>duplicated code</strong>: the two static methods <code class="docutils literal notranslate"><span class="pre">printEmployees(...)</span></code>
(lines 24–28) and <code class="docutils literal notranslate"><span class="pre">printGuests(...)</span></code> (lines 30–34) are pretty much the same,
except for the type of the argument (array of <code class="docutils literal notranslate"><span class="pre">Employee</span></code>s vs. array of
<code class="docutils literal notranslate"><span class="pre">Guest</span></code>s). This suggests that, as the program becomes more complex, we may
need to duplicate the code of all the functionalities that need to work on
both <code class="docutils literal notranslate"><span class="pre">Employee</span></code>s and <code class="docutils literal notranslate"><span class="pre">Guest</span></code>s;</p></li>
<li><p>similarly, we need to keep two distinct arrays for <code class="docutils literal notranslate"><span class="pre">Employee</span></code>s and <code class="docutils literal notranslate"><span class="pre">Guest</span></code>s;
we cannot define an array that contains all people attending the party.</p></li>
</ul>
<p>To address these drawback, we try a different approach, presented in
<a class="reference internal" href="#eg-dtu-christmas-party-one-class-no-interfaces">Example 52</a> below.</p>
</section>
</div><div class="proof example admonition" id="eg-dtu-christmas-party-one-class-no-interfaces">
<p class="admonition-title"><span class="caption-number">Example 52 </span> (DTU Christmas Party — solution 2 (one <code class="docutils literal notranslate"><span class="pre">Person</span></code> class))</p>
<section class="example-content" id="proof-content">
<p>We now address the task by defining one class <code class="docutils literal notranslate"><span class="pre">Person</span></code> (shown below)
representing a generic person attending the Christmas party, with some code
that checks whether the person is an employee or a guest. Notice, in particular:</p>
<ul class="simple">
<li><p>The class <code class="docutils literal notranslate"><span class="pre">Person</span></code> defines a field <code class="docutils literal notranslate"><span class="pre">isEmployee</span></code> which should have the value
<code class="docutils literal notranslate"><span class="pre">true</span></code> in objects that represent an employee, and <code class="docutils literal notranslate"><span class="pre">false</span></code> in objects that
represent a guest;</p></li>
<li><p>Each object of the class <code class="docutils literal notranslate"><span class="pre">Person</span></code> has fields for storing <em>both</em> the person’s
department <em>and</em> the ticket number: these fields are used (or not) depending on
whether the person is an employee or guest (as shown in the body of the method
<code class="docutils literal notranslate"><span class="pre">getInfo()</span></code>);</p></li>
<li><p>we define two constructors:</p>
<ul>
<li><p>one constructor taking a <code class="docutils literal notranslate"><span class="pre">String</span></code> argument for the name, and a <code class="docutils literal notranslate"><span class="pre">String</span></code>
argument for the department. This constructor sets up the object to
represent an employee;</p></li>
<li><p>another constructor taking a <code class="docutils literal notranslate"><span class="pre">String</span></code> argument for the name, and an <code class="docutils literal notranslate"><span class="pre">int</span></code>
argument for the ticket number. This constructor sets up the object to
represent a guest.</p></li>
</ul>
</li>
</ul>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="linenos"> 1</span><span class="kd">class</span> <span class="nc">Person</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 2</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">boolean</span><span class="w"> </span><span class="n">isEmployee</span><span class="p">;</span>
<span class="linenos"> 3</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">name</span><span class="p">;</span>
<span class="linenos"> 4</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">department</span><span class="p">;</span>
<span class="linenos"> 5</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">ticketNumber</span><span class="p">;</span>
<span class="linenos"> 6</span>
<span class="linenos"> 7</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="nf">Person</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">name</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">department</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 8</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">isEmployee</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="p">;</span>
<span class="linenos"> 9</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">name</span><span class="p">;</span>
<span class="linenos">10</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">department</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">department</span><span class="p">;</span>
<span class="linenos">11</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">12</span>
<span class="linenos">13</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="nf">Person</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">name</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">ticketNumber</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">14</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">isEmployee</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="p">;</span>
<span class="linenos">15</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">name</span><span class="p">;</span>
<span class="linenos">16</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">ticketNumber</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ticketNumber</span><span class="p">;</span>
<span class="linenos">17</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">18</span>
<span class="linenos">19</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getInfo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">20</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="na">isEmployee</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">21</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="s">"Employee, "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">department</span><span class="p">;</span>
<span class="linenos">22</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">23</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="s">"Guest, ticket "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">ticketNumber</span><span class="p">;</span>
<span class="linenos">24</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">25</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">26</span><span class="p">}</span>
</pre></div>
</div>
<p>Using the class <code class="docutils literal notranslate"><span class="pre">Person</span></code> above, we can write the following sample program, that
creates a few employees and guests, stores them into arrays, and prints a draft
of the reports outlined in the software requirements.</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="linenos"> 1</span><span class="kd">class</span> <span class="nc">ChristmasParty</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 2</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span><span class="w"> </span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 3</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">e1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Person</span><span class="p">(</span><span class="s">"Ida Iversen"</span><span class="p">,</span><span class="w"> </span><span class="s">"DTU Aqua"</span><span class="p">);</span>
<span class="linenos"> 4</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">e2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Person</span><span class="p">(</span><span class="s">"Oliver Olesen"</span><span class="p">,</span><span class="w"> </span><span class="s">"DTU Construct"</span><span class="p">);</span>
<span class="linenos"> 5</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">e3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Person</span><span class="p">(</span><span class="s">"Alceste Scalas"</span><span class="p">,</span><span class="w"> </span><span class="s">"DTU Compute"</span><span class="p">);</span>
<span class="linenos"> 6</span>
<span class="linenos"> 7</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">g1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Person</span><span class="p">(</span><span class="s">"Laura Lund"</span><span class="p">,</span><span class="w"> </span><span class="mi">123</span><span class="p">);</span>
<span class="linenos"> 8</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">g2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Person</span><span class="p">(</span><span class="s">"Emma Ebbesen"</span><span class="p">,</span><span class="w"> </span><span class="mi">75</span><span class="p">);</span>
<span class="linenos"> 9</span>
<span class="linenos">10</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">people</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Person</span><span class="o">[]</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">e1</span><span class="p">,</span><span class="w"> </span><span class="n">g1</span><span class="p">,</span><span class="w"> </span><span class="n">e2</span><span class="p">,</span><span class="w"> </span><span class="n">e3</span><span class="p">,</span><span class="w"> </span><span class="n">g2</span><span class="w"> </span><span class="p">};</span>
<span class="linenos">11</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">employees</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Person</span><span class="o">[]</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">e1</span><span class="p">,</span><span class="w"> </span><span class="n">e2</span><span class="p">,</span><span class="w"> </span><span class="n">e3</span><span class="w"> </span><span class="p">};</span>
<span class="linenos">12</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">guests</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Person</span><span class="o">[]</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">g1</span><span class="p">,</span><span class="w"> </span><span class="n">g2</span><span class="w"> </span><span class="p">};</span>
<span class="linenos">13</span>
<span class="linenos">14</span><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"List of people at the DTU Christmas party: "</span><span class="p">);</span>
<span class="linenos">15</span><span class="w"> </span><span class="n">printPeople</span><span class="p">(</span><span class="n">people</span><span class="p">);</span>
<span class="linenos">16</span>
<span class="linenos">17</span><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"List of DTU employees at the party: "</span><span class="p">);</span>
<span class="linenos">18</span><span class="w"> </span><span class="n">printPeople</span><span class="p">(</span><span class="n">employees</span><span class="p">);</span>
<span class="linenos">19</span>
<span class="linenos">20</span><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"List of guests at the party: "</span><span class="p">);</span>
<span class="linenos">21</span><span class="w"> </span><span class="n">printPeople</span><span class="p">(</span><span class="n">guests</span><span class="p">);</span>
<span class="linenos">22</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">23</span>
<span class="linenos">24</span><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">printPeople</span><span class="p">(</span><span class="n">Person</span><span class="o">[]</span><span class="w"> </span><span class="n">people</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">25</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">var</span><span class="w"> </span><span class="n">p</span><span class="p">:</span><span class="w"> </span><span class="n">people</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">26</span><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">" - "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="na">name</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">" ("</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="na">getInfo</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">")"</span><span class="p">);</span>
<span class="linenos">27</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">28</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">29</span><span class="p">}</span>
</pre></div>
</div>
<p>The output of this new program satisfies the requirements:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>List of people at the DTU Christmas party:
- Ida Iversen (Employee, DTU Aqua)
- Laura Lund (Guest, ticket 123)
- Oliver Olesen (Employee, DTU Construct)
- Alceste Scalas (Employee, DTU Compute)
- Emma Ebbesen (Guest, ticket 75)
List of DTU employees at the party:
- Ida Iversen (Employee, DTU Aqua)
- Oliver Olesen (Employee, DTU Construct)
- Alceste Scalas (Employee, DTU Compute)
List of guests at the party:
- Laura Lund (Guest, ticket 123)
- Emma Ebbesen (Guest, ticket 75)
</pre></div>
</div>
<p>The code above has less duplication than the solution shown in
<a class="reference internal" href="#eg-dtu-christmas-party-two-classes-no-interfaces">Example 51</a>: there is just one
static method called <code class="docutils literal notranslate"><span class="pre">printPeople(..)</span></code> that handles both employees and guests.
Also, this new solution gives us the flexibility of putting employees and guests
in separate arrays, or in a same array, depending on our needs.</p>
<p>However, this new solution also has two drawbacks:</p>
<ul class="simple">
<li><p>there are <strong>redundant fields in the class <code class="docutils literal notranslate"><span class="pre">Person</span></code></strong>: every object of that
class stores the values of both the person’s department and the ticket number
— but only one of those fields is actually used, depending on whether the
field <code class="docutils literal notranslate"><span class="pre">isEmployee</span></code> is <code class="docutils literal notranslate"><span class="pre">true</span></code> or <code class="docutils literal notranslate"><span class="pre">false</span></code>;</p></li>
<li><p>we have <strong>less type safety</strong>: if we make a slight mistake on lines 10–12
of the static method <code class="docutils literal notranslate"><span class="pre">ChristmasParty.main(...)</span></code>, we may put an employee object
inside the <code class="docutils literal notranslate"><span class="pre">guests</span></code> array (or <em>vice versa</em>, we may put a guest object inside
the <code class="docutils literal notranslate"><span class="pre">employees</span></code> array), and Java will not report any error. We would only
notice the mistake when running the program.</p></li>
</ul>
<p>To address these drawbacks, and the ones discussed in
<a class="reference internal" href="#eg-dtu-christmas-party-two-classes-no-interfaces">Example 51</a>
above,
we examine
<a class="reference internal" href="#sec-interfaces-christmas-solution"><span class="std std-ref">a third solution using Java interfaces</span></a>.</p>
</section>
</div></section>
<section id="implementing-the-dtu-christmas-party-software-using-interfaces">
<span id="sec-interfaces-christmas-solution"></span><h3>Implementing the DTU Christmas Party Software Using <code class="docutils literal notranslate"><span class="pre">interface</span></code>s<a class="headerlink" href="#implementing-the-dtu-christmas-party-software-using-interfaces" title="Permalink to this heading">#</a></h3>
<p>We now explore another approach to the DTU Christmas Party software that
addresses the drawbacks of the solutions shown in
<a class="reference internal" href="#eg-dtu-christmas-party-two-classes-no-interfaces">Example 51</a> and
<a class="reference internal" href="#eg-dtu-christmas-party-one-class-no-interfaces">Example 52</a>.</p>
<p>Observe the classes <code class="docutils literal notranslate"><span class="pre">Employee</span></code> and <code class="docutils literal notranslate"><span class="pre">Guest</span></code> in
<a class="reference internal" href="#eg-dtu-christmas-party-two-classes-no-interfaces">Example 51</a>, and how they are
used in <code class="docutils literal notranslate"><span class="pre">ChristmasParty.java</span></code>. In particular, observe that:</p>
<ul class="simple">
<li><p>both classes <code class="docutils literal notranslate"><span class="pre">Employee</span></code> and <code class="docutils literal notranslate"><span class="pre">Guest</span></code> offer a <code class="docutils literal notranslate"><span class="pre">public</span></code> field called <code class="docutils literal notranslate"><span class="pre">name</span></code>, that
the program <code class="docutils literal notranslate"><span class="pre">ChristmasParty.java</span></code> only uses to read the name of the employee
or guest;</p></li>
<li><p>both classes <code class="docutils literal notranslate"><span class="pre">Employee</span></code> and <code class="docutils literal notranslate"><span class="pre">Guest</span></code> offer a <code class="docutils literal notranslate"><span class="pre">public</span></code> method called
<code class="docutils literal notranslate"><span class="pre">getInfo()</span></code>, that takes no arguments and returns a <code class="docutils literal notranslate"><span class="pre">String</span></code>.</p></li>
</ul>
<p>We can capture this common programming interface by defining a Java <code class="docutils literal notranslate"><span class="pre">interface</span></code>
called e.g. <code class="docutils literal notranslate"><span class="pre">Person</span></code>, and defined as:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="linenos">1</span><span class="kd">interface</span> <span class="nc">Person</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">2</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getName</span><span class="p">();</span>
<span class="linenos">3</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getInfo</span><span class="p">();</span>
<span class="linenos">4</span><span class="p">}</span>
</pre></div>
</div>
<p>Then, we slightly revise the two classes <code class="docutils literal notranslate"><span class="pre">Employee</span></code> and <code class="docutils literal notranslate"><span class="pre">Guest</span></code> in
<a class="reference internal" href="#eg-dtu-christmas-party-two-classes-no-interfaces">Example 51</a>, with two
variations:</p>
<ol class="arabic simple">
<li><p>we declare that both <code class="docutils literal notranslate"><span class="pre">Employee</span></code> and <code class="docutils literal notranslate"><span class="pre">Guest</span></code> implement the methods required by
the interface <code class="docutils literal notranslate"><span class="pre">Person</span></code>, by writing <code class="docutils literal notranslate"><span class="pre">...</span> <span class="pre">implements</span> <span class="pre">Person</span> <span class="pre">...</span></code> in each class
definition;</p></li>
<li><p>we add the implementation of the method <code class="docutils literal notranslate"><span class="pre">getName()</span></code>, which simply returns
the name of the employee or guest.</p></li>
</ol>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The interface <code class="docutils literal notranslate"><span class="pre">Person</span></code> includes the method <code class="docutils literal notranslate"><span class="pre">getName()</span></code> (instead of a field
called <code class="docutils literal notranslate"><span class="pre">name</span></code>) due to a technical constraint: Java <code class="docutils literal notranslate"><span class="pre">interface</span></code>s allow us to
require the presence of specific methods, but they do <em>not</em> allow us to require
the presence of specific fields. Therefore, the only way to write an
<code class="docutils literal notranslate"><span class="pre">interface</span></code> which allows us to retrieve the name of a <code class="docutils literal notranslate"><span class="pre">Person</span></code> is to include a
method like <code class="docutils literal notranslate"><span class="pre">getName()</span></code>.</p>
</div>
<p>The resulting updated classes are shown below: the differences with
<a class="reference internal" href="#eg-dtu-christmas-party-two-classes-no-interfaces">Example 51</a> are highlighted.</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="hll"><span class="linenos"> 1</span><span class="kd">class</span> <span class="nc">Employee</span><span class="w"> </span><span class="kd">implements</span><span class="w"> </span><span class="n">Person</span><span class="w"> </span><span class="p">{</span>
</span><span class="linenos"> 2</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">name</span><span class="p">;</span>
<span class="linenos"> 3</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">department</span><span class="p">;</span>
<span class="linenos"> 4</span>
<span class="linenos"> 5</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="nf">Employee</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">name</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">department</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 6</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">name</span><span class="p">;</span>
<span class="linenos"> 7</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">department</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">department</span><span class="p">;</span>
<span class="linenos"> 8</span><span class="w"> </span><span class="p">}</span>
<span class="linenos"> 9</span>
<span class="hll"><span class="linenos">10</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getName</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
</span><span class="hll"><span class="linenos">11</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">name</span><span class="p">;</span>
</span><span class="hll"><span class="linenos">12</span><span class="w"> </span><span class="p">}</span>
</span><span class="linenos">13</span>
<span class="linenos">14</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getInfo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">15</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="s">"Employee, "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">department</span><span class="p">;</span>
<span class="linenos">16</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">17</span><span class="p">}</span>
<span class="linenos">18</span>
<span class="hll"><span class="linenos">19</span><span class="kd">class</span> <span class="nc">Guest</span><span class="w"> </span><span class="kd">implements</span><span class="w"> </span><span class="n">Person</span><span class="w"> </span><span class="p">{</span>
</span><span class="linenos">20</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">name</span><span class="p">;</span>
<span class="linenos">21</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">ticketNumber</span><span class="p">;</span>
<span class="linenos">22</span>
<span class="linenos">23</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="nf">Guest</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">name</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">ticketNumber</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">24</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">name</span><span class="p">;</span>
<span class="linenos">25</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">ticketNumber</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ticketNumber</span><span class="p">;</span>
<span class="linenos">26</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">27</span>
<span class="hll"><span class="linenos">28</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getName</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
</span><span class="hll"><span class="linenos">29</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">name</span><span class="p">;</span>
</span><span class="hll"><span class="linenos">30</span><span class="w"> </span><span class="p">}</span>
</span><span class="linenos">31</span>
<span class="linenos">32</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getInfo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="linenos">33</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="s">"Guest, ticket "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">ticketNumber</span><span class="p">;</span>
<span class="linenos">34</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">35</span><span class="p">}</span>
</pre></div>
</div>
<p>The relationship between the interface <code class="docutils literal notranslate"><span class="pre">Person</span></code> and the revised classes
<code class="docutils literal notranslate"><span class="pre">Employee</span></code> and <code class="docutils literal notranslate"><span class="pre">Guest</span></code> is graphically depicted in the
<a class="reference external" href="https://www.visual-paradigm.com/guide/uml-unified-modeling-language/uml-class-diagram-tutorial/">UML class diagram</a>
in <a class="reference internal" href="#fig-uml-person-employee-guest"><span class="std std-numref">Fig. 19</span></a> below.</p>
<figure class="align-default" id="fig-uml-person-employee-guest">
<img alt="UML class diagram of the interface 'Person' and classes 'Person' and 'Employee'" src="_images/person-employee-guest.svg" /><figcaption>
<p><span class="caption-number">Fig. 19 </span><span class="caption-text">UML class diagram showing the relationship between the interface <code class="docutils literal notranslate"><span class="pre">Person</span></code> and
the revised classes <code class="docutils literal notranslate"><span class="pre">Employee</span></code> and <code class="docutils literal notranslate"><span class="pre">Guest</span></code>.</span><a class="headerlink" href="#fig-uml-person-employee-guest" title="Permalink to this image">#</a></p>
</figcaption>
</figure>
<p>The diagram in <a class="reference internal" href="#fig-uml-person-employee-guest"><span class="std std-numref">Fig. 19</span></a> is interpreted as
follows:</p>
<ul class="simple">
<li><p>the interface <code class="docutils literal notranslate"><span class="pre">Person</span></code> at the top shows the abstract methods required by the
interface definition, with the corresponding arguments (if any) and return
types. For instance: the interface <code class="docutils literal notranslate"><span class="pre">Person</span></code> specifies a method <code class="docutils literal notranslate"><span class="pre">getName()</span></code>
that takes no arguments and returns a <code class="docutils literal notranslate"><span class="pre">String</span></code>;</p></li>
<li><p>the classes <code class="docutils literal notranslate"><span class="pre">Employee</span></code> and <code class="docutils literal notranslate"><span class="pre">Guest</span></code> show their respective constructors and
methods (if any), with their respective argument types. For instance: the
constructor of <code class="docutils literal notranslate"><span class="pre">Employee</span></code> takes two arguments (<code class="docutils literal notranslate"><span class="pre">name</span></code> and <code class="docutils literal notranslate"><span class="pre">department</span></code>) both
having type <code class="docutils literal notranslate"><span class="pre">String</span></code>;</p></li>
<li><p>the dashed arrows mean that the class at the origin of the arrow <em>implements</em>
the interface pointed by the arrow (e.g. the class <code class="docutils literal notranslate"><span class="pre">Employee</span></code> implements the
interface <code class="docutils literal notranslate"><span class="pre">Person</span></code>, hence <code class="docutils literal notranslate"><span class="pre">Employee</span></code> must implement the methods <code class="docutils literal notranslate"><span class="pre">getName()</span></code>
and <code class="docutils literal notranslate"><span class="pre">getInfo()</span></code>, even if they are not shown explicitly in the class diagram).</p></li>
</ul>
<p>Now, with the definitions above, we can use <code class="docutils literal notranslate"><span class="pre">Person</span></code> as a type: in fact,
<strong><code class="docutils literal notranslate"><span class="pre">Person</span></code> is <em>the type of all objects of any class which implements the interface
<code class="docutils literal notranslate"><span class="pre">Person</span></code></em></strong>. Therefore, any object of the class <code class="docutils literal notranslate"><span class="pre">Employee</span></code> has also type
<code class="docutils literal notranslate"><span class="pre">Person</span></code>, and any object of the class <code class="docutils literal notranslate"><span class="pre">Guest</span></code> has also type <code class="docutils literal notranslate"><span class="pre">Person</span></code>.</p>
<p>Consequently, using the interface <code class="docutils literal notranslate"><span class="pre">Person</span></code> and the revised classes <code class="docutils literal notranslate"><span class="pre">Employee</span></code>
and <code class="docutils literal notranslate"><span class="pre">Guest</span></code> above, we can revise the program <code class="docutils literal notranslate"><span class="pre">ChristmasParty.java</span></code> in
<a class="reference internal" href="#eg-dtu-christmas-party-two-classes-no-interfaces">Example 51</a> and make it very
similar to the simpler version discussed in
<a class="reference internal" href="#eg-dtu-christmas-party-one-class-no-interfaces">Example 52</a>: the result is shown
below. (The differences with
<a class="reference internal" href="#eg-dtu-christmas-party-one-class-no-interfaces">Example 52</a> are highlighted.)</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="linenos"> 1</span><span class="kd">class</span> <span class="nc">ChristmasParty</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 2</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span><span class="w"> </span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 3</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">e1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Employee</span><span class="p">(</span><span class="s">"Ida Iversen"</span><span class="p">,</span><span class="w"> </span><span class="s">"DTU Aqua"</span><span class="p">);</span>
<span class="linenos"> 4</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">e2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Employee</span><span class="p">(</span><span class="s">"Oliver Olesen"</span><span class="p">,</span><span class="w"> </span><span class="s">"DTU Construct"</span><span class="p">);</span>
<span class="linenos"> 5</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">e3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Employee</span><span class="p">(</span><span class="s">"Alceste Scalas"</span><span class="p">,</span><span class="w"> </span><span class="s">"DTU Compute"</span><span class="p">);</span>
<span class="linenos"> 6</span>
<span class="linenos"> 7</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">g1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Guest</span><span class="p">(</span><span class="s">"Laura Lund"</span><span class="p">,</span><span class="w"> </span><span class="mi">123</span><span class="p">);</span>
<span class="linenos"> 8</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">g2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Guest</span><span class="p">(</span><span class="s">"Emma Ebbesen"</span><span class="p">,</span><span class="w"> </span><span class="mi">75</span><span class="p">);</span>
<span class="linenos"> 9</span>
<span class="hll"><span class="linenos">10</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">people</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Person</span><span class="o">[]</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">e1</span><span class="p">,</span><span class="w"> </span><span class="n">g1</span><span class="p">,</span><span class="w"> </span><span class="n">e2</span><span class="p">,</span><span class="w"> </span><span class="n">e3</span><span class="p">,</span><span class="w"> </span><span class="n">g2</span><span class="w"> </span><span class="p">};</span>
</span><span class="hll"><span class="linenos">11</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">employees</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Employee</span><span class="o">[]</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">e1</span><span class="p">,</span><span class="w"> </span><span class="n">e2</span><span class="p">,</span><span class="w"> </span><span class="n">e3</span><span class="w"> </span><span class="p">};</span>
</span><span class="hll"><span class="linenos">12</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">guests</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Guest</span><span class="o">[]</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">g1</span><span class="p">,</span><span class="w"> </span><span class="n">g2</span><span class="w"> </span><span class="p">};</span>
</span><span class="linenos">13</span>
<span class="linenos">14</span><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"List of people at the DTU Christmas party: "</span><span class="p">);</span>
<span class="hll"><span class="linenos">15</span><span class="w"> </span><span class="n">printPeople</span><span class="p">(</span><span class="n">people</span><span class="p">);</span>
</span><span class="linenos">16</span>
<span class="linenos">17</span><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"List of DTU employees at the party: "</span><span class="p">);</span>
<span class="hll"><span class="linenos">18</span><span class="w"> </span><span class="n">printPeople</span><span class="p">(</span><span class="n">employees</span><span class="p">);</span>
</span><span class="linenos">19</span>
<span class="linenos">20</span><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">"List of guests at the party: "</span><span class="p">);</span>
<span class="hll"><span class="linenos">21</span><span class="w"> </span><span class="n">printPeople</span><span class="p">(</span><span class="n">guests</span><span class="p">);</span>
</span><span class="linenos">22</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">23</span>
<span class="hll"><span class="linenos">24</span><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">printPeople</span><span class="p">(</span><span class="n">Person</span><span class="o">[]</span><span class="w"> </span><span class="n">people</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
</span><span class="hll"><span class="linenos">25</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">var</span><span class="w"> </span><span class="n">p</span><span class="p">:</span><span class="w"> </span><span class="n">people</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
</span><span class="hll"><span class="linenos">26</span><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="na">out</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="s">" - "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="na">getName</span><span class="p">()</span>
</span><span class="hll"><span class="linenos">27</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">" ("</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="na">getInfo</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">")"</span><span class="p">);</span>
</span><span class="hll"><span class="linenos">28</span><span class="w"> </span><span class="p">}</span>
</span><span class="hll"><span class="linenos">29</span><span class="w"> </span><span class="p">}</span>
</span><span class="linenos">30</span><span class="p">}</span>
</pre></div>
</div>
<p>Observe that this new program addresses the drawbacks of both
<a class="reference internal" href="#eg-dtu-christmas-party-two-classes-no-interfaces">Example 51</a> and
<a class="reference internal" href="#eg-dtu-christmas-party-one-class-no-interfaces">Example 52</a>:</p>
<ul class="simple">
<li><p>we can now create an array of type <code class="docutils literal notranslate"><span class="pre">Person[]</span></code> (line 10), i.e. an array that
contains any object offering the methods of the interface <code class="docutils literal notranslate"><span class="pre">Person</span></code>.
Therefore, the array <code class="docutils literal notranslate"><span class="pre">people</span></code> can contain both objects of the class
<code class="docutils literal notranslate"><span class="pre">Employee</span></code>, and objects of the class <code class="docutils literal notranslate"><span class="pre">Guest</span></code>;</p></li>
<li><p>we can also define arrays containing elements of the class <code class="docutils literal notranslate"><span class="pre">Employee</span></code> <em>only</em>,
or <code class="docutils literal notranslate"><span class="pre">Guest</span></code> <em>only</em> (lines 11–12): if we make a mistake and try to put e.g. the
employee <code class="docutils literal notranslate"><span class="pre">e1</span></code> inside the array <code class="docutils literal notranslate"><span class="pre">guests</span></code>, Java will report a type error, and
the program will not compile;</p></li>
<li><p>we can also define methods that take as arguments any object (or array of
objects) implementing the interface <code class="docutils literal notranslate"><span class="pre">Person</span></code>. In particular,
<code class="docutils literal notranslate"><span class="pre">printPeople(...)</span></code> (lines 24–29) takes as argument an array of objects that
implement the interface <code class="docutils literal notranslate"><span class="pre">Person</span></code>, and only uses the methods provided by that
interface (i.e. <code class="docutils literal notranslate"><span class="pre">getName()</span></code> and <code class="docutils literal notranslate"><span class="pre">getInfo()</span></code>). Consequently,
<code class="docutils literal notranslate"><span class="pre">printPeople(...)</span></code> is very reusable: the program calls it three times with
arrays containing <code class="docutils literal notranslate"><span class="pre">Person</span></code>s (line 15), <code class="docutils literal notranslate"><span class="pre">Employee</span></code>s (line 18), and <code class="docutils literal notranslate"><span class="pre">Guest</span></code>s
(line 21).</p></li>
</ul>
<p>Notice that method calls on objects of type <code class="docutils literal notranslate"><span class="pre">Person</span></code>, like <code class="docutils literal notranslate"><span class="pre">p.getInfo()</span></code>
(line 27), will call the method provided by the object in <code class="docutils literal notranslate"><span class="pre">p</span></code>: therefore, if the
object in <code class="docutils literal notranslate"><span class="pre">p</span></code> is a <code class="docutils literal notranslate"><span class="pre">Guest</span></code>, then <code class="docutils literal notranslate"><span class="pre">p.getInfo()</span></code> will call the method <code class="docutils literal notranslate"><span class="pre">getInfo()</span></code>
of the class <code class="docutils literal notranslate"><span class="pre">Guest</span></code>, which returns a string like
<code class="docutils literal notranslate"><span class="pre">"Laura</span> <span class="pre">Lund</span> <span class="pre">(Guest,</span> <span class="pre">ticket</span> <span class="pre">123)"</span></code>.</p>
<p>The output of the program above is the following, and respects the requirements:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>List of people at the DTU Christmas party:
- Ida Iversen (Employee, DTU Aqua)
- Laura Lund (Guest, ticket 123)
- Oliver Olesen (Employee, DTU Construct)
- Alceste Scalas (Employee, DTU Compute)
- Emma Ebbesen (Guest, ticket 75)
List of DTU employees at the party:
- Ida Iversen (Employee, DTU Aqua)
- Oliver Olesen (Employee, DTU Construct)
- Alceste Scalas (Employee, DTU Compute)
List of guests at the party:
- Laura Lund (Guest, ticket 123)
- Emma Ebbesen (Guest, ticket 75)
</pre></div>
</div>
</section>
<section id="extending-interfaces-and-implementing-multiple-interfaces">
<span id="sec-interfaces-electronics"></span><h3>Extending Interfaces and Implementing Multiple Interfaces<a class="headerlink" href="#extending-interfaces-and-implementing-multiple-interfaces" title="Permalink to this heading">#</a></h3>
<p>Our Java code can define and use as many <code class="docutils literal notranslate"><span class="pre">interface</span></code>s as we need. We can also
combine the interfaces in two ways:</p>
<ul>
<li><p>We can <strong>extend an interface</strong>. For example, if we define:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">interface</span> <span class="nc">CommercialDriver</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">Person</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">getLicenseNumber</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
<p>then the interface <code class="docutils literal notranslate"><span class="pre">CommercialDriver</span></code> has all the methods of <code class="docutils literal notranslate"><span class="pre">Person</span></code>, <em>plus</em>
the method <code class="docutils literal notranslate"><span class="pre">getLicenseNumber()</span></code>. Then, if we define a class like e.g.:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">class</span> <span class="nc">TaxiDriver</span><span class="w"> </span><span class="kd">implements</span><span class="w"> </span><span class="n">CommercialDriver</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Then the class <code class="docutils literal notranslate"><span class="pre">TaxiDriver</span></code> must implement all the methods of
<code class="docutils literal notranslate"><span class="pre">CommercialDriver</span></code> (including those of <code class="docutils literal notranslate"><span class="pre">Person</span></code>); moreover, each object of the
class <code class="docutils literal notranslate"><span class="pre">TaxiDriver</span></code> has <em>also</em> the types <code class="docutils literal notranslate"><span class="pre">CommercialDriver</span></code> and <code class="docutils literal notranslate"><span class="pre">Person</span></code>.</p>
</li>
<li><p>We can <strong>write classes that implement multiple interfaces</strong>. For example, if
we define:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">interface</span> <span class="nc">HasGpsTag</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">double</span><span class="w"> </span><span class="nf">getDistanceFrom</span><span class="p">(</span><span class="kt">double</span><span class="w"> </span><span class="n">latitude</span><span class="p">,</span><span class="w"> </span><span class="kt">double</span><span class="w"> </span><span class="n">longitude</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>then we can define classes that implement <em>both</em> the methods of <code class="docutils literal notranslate"><span class="pre">Person</span></code> <em>and</em>
<code class="docutils literal notranslate"><span class="pre">HasGpsTag</span></code>, like:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">class</span> <span class="nc">PoliceAgent</span><span class="w"> </span><span class="kd">implements</span><span class="w"> </span><span class="n">Person</span><span class="p">,</span><span class="w"> </span><span class="n">HasGpsTag</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Moreover, each object of the class <code class="docutils literal notranslate"><span class="pre">PoliceAgent</span></code> has <em>also</em> the types
<code class="docutils literal notranslate"><span class="pre">Person</span></code> and <code class="docutils literal notranslate"><span class="pre">HasGpsTag</span></code>.</p>
</li>
</ul>
<p><a class="reference internal" href="#eg-multiple-interfaces">Example 53</a> below shows both these features in action.</p>
<div class="proof example admonition" id="eg-multiple-interfaces">
<p class="admonition-title"><span class="caption-number">Example 53 </span> (Office supplies shop)</p>
<section class="example-content" id="proof-content">
<p>We are asked to write the software that manages the inventory of a shop that
sells office supplies — in particular, scanners and printers. The
requirements are:</p>
<ul class="simple">
<li><p>every device sold by the shop has a model, a description, and a serial code;</p></li>
<li><p>the shop handles three kinds of devices with different functionalities:</p>
<ul>
<li><p>scanners can scan, and we need to store and retrieve their scanning
resolution in DPI (dots per inch);</p></li>
<li><p>printers can print, and we need to store and retrieve their printing
resolution in DPI;</p></li>
<li><p>all-in-one devices can both scan and print, and we need to store and
retrieve both their scanning and printing resolutions in DPI.</p></li>
</ul>
</li>
</ul>
<p>The software we are going to write must handle the devices according to their
scanning and printing functionalities.</p>
<p>After some analysis, we decide to organise our program with:</p>
<ul class="simple">
<li><p>a basic interface <code class="docutils literal notranslate"><span class="pre">Device</span></code> with methods to get the model, description, and
serial code of a device;</p></li>
<li><p>an interface <code class="docutils literal notranslate"><span class="pre">CanScan</span></code> that extends <code class="docutils literal notranslate"><span class="pre">Device</span></code> with a method to get the scanning
resolution;</p></li>
<li><p>an interface <code class="docutils literal notranslate"><span class="pre">CanPrint</span></code> that extends <code class="docutils literal notranslate"><span class="pre">Device</span></code> with a method to get the
printing resolution;</p></li>
<li><p>three classes representing the three kinds of devices sold by the shop, and
their functionalities:</p>
<ul>
<li><p><code class="docutils literal notranslate"><span class="pre">Scanner</span></code>, implementing the interface <code class="docutils literal notranslate"><span class="pre">CanScan</span></code>;</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">Printer</span></code>, implementing the interface <code class="docutils literal notranslate"><span class="pre">CanPrint</span></code>;</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">AllInOne</span></code>, implementing both interfaces <code class="docutils literal notranslate"><span class="pre">CanScan</span></code> and <code class="docutils literal notranslate"><span class="pre">CanPrint</span></code>.</p></li>
</ul>
</li>
</ul>
<p>The corresponding UML class diagram is shown in
<a class="reference internal" href="#fig-uml-office-supplies"><span class="std std-numref">Fig. 20</span></a> below, where:</p>
<ul class="simple">
<li><p>each interface and class specifies its methods, with argument types and return
types;</p></li>
<li><p>the solid arrows show when an interface (the origin of the arrow) <em>extends</em>
another interface (the target of the arrow);</p></li>
<li><p>the dashed arrows show when a class (the origin of the arrow) <em>implements</em> one
or more interfaces (the target(s) of the arrow).</p></li>
</ul>
<figure class="align-default" id="fig-uml-office-supplies">
<img alt="UML class diagram for the office supplies shop software" src="_images/office-supplies.svg" /><figcaption>
<p><span class="caption-number">Fig. 20 </span><span class="caption-text">UML class diagram showing the relationship between the interfaces and classes
of the “office supplies shop” example. Observe that the interfaces <code class="docutils literal notranslate"><span class="pre">CanScan</span></code>
and <code class="docutils literal notranslate"><span class="pre">CanPrint</span></code> extend the basic interface <code class="docutils literal notranslate"><span class="pre">Device</span></code>, and the class <code class="docutils literal notranslate"><span class="pre">AllInOne</span></code>
implements two interfaces.</span><a class="headerlink" href="#fig-uml-office-supplies" title="Permalink to this image">#</a></p>
</figcaption>
</figure>
<p>The corresponding interface and class definitions are shown below. Notice that
we keep all fields <code class="docutils literal notranslate"><span class="pre">private</span></code>, but their value can be retrieved by calling the
appropriate interface methods.</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="linenos"> 1</span><span class="kd">interface</span> <span class="nc">Device</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 2</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getModel</span><span class="p">();</span>
<span class="linenos"> 3</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getDescription</span><span class="p">();</span>
<span class="linenos"> 4</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getSerialCode</span><span class="p">();</span>
<span class="linenos"> 5</span><span class="p">}</span>
<span class="linenos"> 6</span>
<span class="linenos"> 7</span><span class="kd">interface</span> <span class="nc">CanScan</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">Device</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 8</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">getScannerDPI</span><span class="p">();</span>
<span class="linenos"> 9</span><span class="p">}</span>
<span class="linenos"> 10</span>
<span class="linenos"> 11</span><span class="kd">interface</span> <span class="nc">CanPrint</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">Device</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 12</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">getPrinterDPI</span><span class="p">();</span>
<span class="linenos"> 13</span><span class="p">}</span>
<span class="linenos"> 14</span>
<span class="linenos"> 15</span><span class="kd">class</span> <span class="nc">Scanner</span><span class="w"> </span><span class="kd">implements</span><span class="w"> </span><span class="n">CanScan</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 16</span><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">model</span><span class="p">;</span>
<span class="linenos"> 17</span><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">dpi</span><span class="p">;</span>
<span class="linenos"> 18</span><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">serialCode</span><span class="p">;</span>
<span class="linenos"> 19</span>
<span class="linenos"> 20</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="nf">Scanner</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">model</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">dpi</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">serialCode</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 21</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">model</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">model</span><span class="p">;</span>
<span class="linenos"> 22</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">dpi</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dpi</span><span class="p">;</span>
<span class="linenos"> 23</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">serialCode</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">serialCode</span><span class="p">;</span>
<span class="linenos"> 24</span><span class="w"> </span><span class="p">}</span>
<span class="linenos"> 25</span>
<span class="linenos"> 26</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getModel</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 27</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">model</span><span class="p">;</span>
<span class="linenos"> 28</span><span class="w"> </span><span class="p">}</span>
<span class="linenos"> 29</span>
<span class="linenos"> 30</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getSerialCode</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 31</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">serialCode</span><span class="p">;</span>
<span class="linenos"> 32</span><span class="w"> </span><span class="p">}</span>
<span class="linenos"> 33</span>
<span class="linenos"> 34</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">getScannerDPI</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 35</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">dpi</span><span class="p">;</span>
<span class="linenos"> 36</span><span class="w"> </span><span class="p">}</span>
<span class="linenos"> 37</span>
<span class="linenos"> 38</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getDescription</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 39</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="s">"Scanner"</span><span class="p">;</span>
<span class="linenos"> 40</span><span class="w"> </span><span class="p">}</span>
<span class="linenos"> 41</span><span class="p">}</span>
<span class="linenos"> 42</span>
<span class="linenos"> 43</span><span class="kd">class</span> <span class="nc">Printer</span><span class="w"> </span><span class="kd">implements</span><span class="w"> </span><span class="n">CanPrint</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 44</span><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">model</span><span class="p">;</span>
<span class="linenos"> 45</span><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">dpi</span><span class="p">;</span>
<span class="linenos"> 46</span><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">serialCode</span><span class="p">;</span>
<span class="linenos"> 47</span>
<span class="linenos"> 48</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="nf">Printer</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">model</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">dpi</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">serialCode</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 49</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">model</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">model</span><span class="p">;</span>
<span class="linenos"> 50</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">dpi</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dpi</span><span class="p">;</span>
<span class="linenos"> 51</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">serialCode</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">serialCode</span><span class="p">;</span>
<span class="linenos"> 52</span><span class="w"> </span><span class="p">}</span>
<span class="linenos"> 53</span>
<span class="linenos"> 54</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getModel</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 55</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">model</span><span class="p">;</span>
<span class="linenos"> 56</span><span class="w"> </span><span class="p">}</span>
<span class="linenos"> 57</span>
<span class="linenos"> 58</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getSerialCode</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 59</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">serialCode</span><span class="p">;</span>
<span class="linenos"> 60</span><span class="w"> </span><span class="p">}</span>
<span class="linenos"> 61</span>
<span class="linenos"> 62</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">getPrinterDPI</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 63</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">dpi</span><span class="p">;</span>
<span class="linenos"> 64</span><span class="w"> </span><span class="p">}</span>
<span class="linenos"> 65</span>
<span class="linenos"> 66</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getDescription</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 67</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="s">"Printer"</span><span class="p">;</span>
<span class="linenos"> 68</span><span class="w"> </span><span class="p">}</span>
<span class="linenos"> 69</span><span class="p">}</span>
<span class="linenos"> 70</span>
<span class="linenos"> 71</span><span class="kd">class</span> <span class="nc">AllInOne</span><span class="w"> </span><span class="kd">implements</span><span class="w"> </span><span class="n">CanScan</span><span class="p">,</span><span class="w"> </span><span class="n">CanPrint</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 72</span><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">model</span><span class="p">;</span>
<span class="linenos"> 73</span><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">scannerDpi</span><span class="p">;</span>
<span class="linenos"> 74</span><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">printerDpi</span><span class="p">;</span>
<span class="linenos"> 75</span><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">serialCode</span><span class="p">;</span>
<span class="linenos"> 76</span>
<span class="linenos"> 77</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="nf">AllInOne</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">model</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">scanDpi</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">printDpi</span><span class="p">,</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">serialCode</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 78</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">model</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">model</span><span class="p">;</span>
<span class="linenos"> 79</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">scannerDpi</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">scanDpi</span><span class="p">;</span>
<span class="linenos"> 80</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">printerDpi</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">printDpi</span><span class="p">;</span>