-
Notifications
You must be signed in to change notification settings - Fork 0
/
inheritance-oo.html
1070 lines (949 loc) · 95.1 KB
/
inheritance-oo.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 9, Part 2: Class Inheritance and Principles of Object-Oriented Programming — 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 10, part 1: More on Class Inheritance and abstract classes" href="inheritance-2.html" />
<link rel="prev" title="Module 9, Part 1: Lab Day" href="lab-day-4.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"><a class="reference internal" href="interfaces.html">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 current active"><a class="current reference internal" href="#">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 9, Part 2: Class Inheritance and Principles of Object-Oriented Programming</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="#class-inheritance-defining-a-class-by-extending-another-class">Class Inheritance: Defining a Class by Extending Another Class</a><ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#fundamentals">Fundamentals</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#calling-a-super-constructor">Calling a “<code class="docutils literal notranslate"><span class="pre">super</span></code>” Constructor</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#overriding-methods">Overriding Methods</a></li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#what-is-object-oriented-programming">What is “Object-Oriented Programming”?</a></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="#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="#vending-machines-version-2">06 - Vending Machines, Version 2</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#vehicle-fleet-management-version-2">07 - Vehicle Fleet Management, Version 2</a></li>
<li class="toc-h3 nav-item toc-entry"><a class="reference internal nav-link" href="#pets">08 - Pets</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-9-part-2-class-inheritance-and-principles-of-object-oriented-programming">
<span id="mod-inheritance-oo"></span><h1>Module 9, Part 2: Class Inheritance and Principles of Object-Oriented Programming<a class="headerlink" href="#module-9-part-2-class-inheritance-and-principles-of-object-oriented-programming" title="Permalink to this heading">#</a></h1>
<p>In this second part of Module 9 we explore
<a class="reference internal" href="#sec-inheritance-oo-inheritance"><span class="std std-ref">how we can extend classes using inheritance</span></a>
to improve the structure and reusability
of our Java code.
We then discuss <a class="reference internal" href="#sec-inheritance-oo-oo"><span class="std std-ref">the main principles of object-oriented programming</span></a>.</p>
<div class="admonition important">
<p class="admonition-title">Important</p>
<p>To try the examples described in this Module, it is highly recommended to
<a class="reference internal" href="prog-interf-encapsulation.html#sec-interfaces-encapsulation-vs-code-java"><span class="std std-ref">install and use the Java Extension Pack for Visual Studio Code</span></a>.</p>
</div>
<span class="target" id="index-0"></span><section id="class-inheritance-defining-a-class-by-extending-another-class">
<span id="sec-inheritance-oo-inheritance"></span><span id="index-1"></span><h2>Class Inheritance: Defining a Class by Extending Another Class<a class="headerlink" href="#class-inheritance-defining-a-class-by-extending-another-class" title="Permalink to this heading">#</a></h2>
<p>In this section explore <strong>inheritance</strong> in Java. The basic idea is quite simple
(i.e. define a class by extending another class) — but this simple idea comes
with many nuanced details, and it has important ramifications in the way we can
design our Java programs, and how the programs behave when executed. In the
next subsections we address the following topics.</p>
<ul class="simple">
<li><p><a class="reference internal" href="#sec-inheritance-oo-inheritance-fundamentals"><span class="std std-ref">Fundamentals</span></a></p></li>
<li><p><a class="reference internal" href="#sec-inheritance-oo-inheritance-super-constructor"><span class="std std-ref">Calling a “super” Constructor</span></a></p></li>
<li><p><a class="reference internal" href="#sec-inheritance-oo-inheritance-overriding-methods"><span class="std std-ref">Overriding Methods</span></a></p></li>
</ul>
<section id="fundamentals">
<span id="sec-inheritance-oo-inheritance-fundamentals"></span><h3>Fundamentals<a class="headerlink" href="#fundamentals" title="Permalink to this heading">#</a></h3>
<p>In Java, we can specify that a class <strong>extends</strong> another class:</p>
<ul class="simple">
<li><p>the class that performs the extension is called <strong>subclass</strong> or <strong>derived class</strong>;</p></li>
<li><p>the class that is extended is called <strong>superclass</strong> or <strong>parent class</strong></p></li>
</ul>
<p>The subclass <strong>inherits</strong> all the fields and methods of the superclass: the idea
is that inherited fields and methods are available to the code written in the
subclass, and they can be accessed and called through the object of the
subclass — as if they were defined in the subclass itself. The only exception
are the fields and methods that are declared <code class="docutils literal notranslate"><span class="pre">private</span></code> in the superclass: they
are <em>not</em> accessible outside the superclass.</p>
<p>Inheritance allows us to maximise code reuse between the superclass and the
subclass(es); moreover, <strong>each object of the subclass is <em>also</em> an object of the
superclass</strong>, which further increases code flexibility and reusability. This is
illustrated in <a class="reference internal" href="#eg-inheritance-pets-cats-dogs">Example 54</a> and
<a class="reference internal" href="#eg-inheritance-pets-cats-dogs-subclass-superclass">Example 55</a> below.</p>
<div class="admonition important">
<p class="admonition-title">Important</p>
<ul class="simple">
<li><p>A subclass does <em>not</em> inherit the constructor(s) of its superclass. We will
discuss this shortly, when introducing <a class="reference internal" href="#sec-inheritance-oo-inheritance-super-constructor"><span class="std std-ref">“super” constructor calls</span></a>.</p></li>
<li><p>Some key differences between class inheritance and using <a class="reference internal" href="interfaces.html#sec-interfaces-interfaces"><span class="std std-ref">Java interfaces</span></a>:</p>
<ul>
<li><p>A class can implement multiple interfaces, but it can only extend <em>one</em>
other class.</p></li>
<li><p>An interface cannot declare object fields, whereas a class can
declare both fields and methods — and if they can be inherited by the
subclasses.</p></li>
</ul>
</li>
</ul>
</div>
<div class="proof example admonition" id="eg-inheritance-pets-cats-dogs">
<p class="admonition-title"><span class="caption-number">Example 54 </span> (Basic example of class inheritance: pets, cats, and dogs)</p>
<section class="example-content" id="proof-content">
<p>Suppose we need to implement a program that handles pets of various kinds, e.g.
cats and dogs. Each pet has a name and a species, and must provide a
description. We can approach the task by defining a generic class <code class="docutils literal notranslate"><span class="pre">Pet</span></code> that
captures all the common features of the various kinds of pets — i.e. the class
<code class="docutils literal notranslate"><span class="pre">Pet</span></code> has fields for the name and species, and a method for returning a
description, as follows:</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">Pet</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">species</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="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">6</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="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="k">this</span><span class="p">.</span><span class="na">species</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">")"</span><span class="p">;</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>Then we can define the <strong>sub-classes</strong> (a.k.a. <strong>derived classes</strong>) <code class="docutils literal notranslate"><span class="pre">Dog</span></code> and
<code class="docutils literal notranslate"><span class="pre">Cat</span></code>, that <em>extend</em> <code class="docutils literal notranslate"><span class="pre">Pet</span></code>:</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">Dog</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">Pet</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="nf">Dog</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="p">{</span>
<span class="linenos"> 3</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"> 4</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">species</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"dog"</span><span class="p">;</span>
<span class="linenos"> 5</span><span class="w"> </span><span class="p">}</span>
<span class="linenos"> 6</span><span class="p">}</span>
<span class="linenos"> 7</span>
<span class="linenos"> 8</span><span class="kd">class</span> <span class="nc">Cat</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">Pet</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 9</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="nf">Cat</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="p">{</span>
<span class="linenos">10</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">11</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">species</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">"cat"</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>
</pre></div>
</div>
<p>By writing e.b. <code class="docutils literal notranslate"><span class="pre">class</span> <span class="pre">Dog</span> <span class="pre">extends</span> <span class="pre">Pet</span> <span class="pre">...</span></code>, we are declaring two things:</p>
<ul class="simple">
<li><p>each (non-<code class="docutils literal notranslate"><span class="pre">private</span></code>) field and method of <code class="docutils literal notranslate"><span class="pre">Pet</span></code> is also a field/method of
<code class="docutils literal notranslate"><span class="pre">Dog</span></code>. In fact, you can notice that the constructor of <code class="docutils literal notranslate"><span class="pre">Dog</span></code> initialises the
fields <code class="docutils literal notranslate"><span class="pre">this.name</span></code> and <code class="docutils literal notranslate"><span class="pre">this.species</span></code> (lines 3–4), and those fields are
defined in <code class="docutils literal notranslate"><span class="pre">Pet</span></code>. Moreover,</p></li>
<li><p>each object of the class <code class="docutils literal notranslate"><span class="pre">Dog</span></code> is <em>also</em> an object of the class <code class="docutils literal notranslate"><span class="pre">Pet</span></code>.</p></li>
</ul>
<p>(Similar considerations apply to the class <code class="docutils literal notranslate"><span class="pre">Cat</span></code>, since it also extends <code class="docutils literal notranslate"><span class="pre">Pet</span></code>).</p>
<p>The UML class diagram of the classes <code class="docutils literal notranslate"><span class="pre">Pet</span></code>, <code class="docutils literal notranslate"><span class="pre">Dog</span></code>, and <code class="docutils literal notranslate"><span class="pre">Cat</span></code> is shown
in <a class="reference internal" href="#fig-uml-pets-cat-dog-only"><span class="std std-numref">Fig. 21</span></a> below, where:</p>
<ul class="simple">
<li><p>each class specifies its <code class="docutils literal notranslate"><span class="pre">public</span></code> fields and methods (the latter with argument
types and return types);</p></li>
<li><p>the solid arrows show when a class (the origin of the arrow) <em>extends</em>
another class (the target of the arrow).</p></li>
</ul>
<figure class="align-default" id="fig-uml-pets-cat-dog-only">
<img alt="UML class diagram for the pet classes" src="_images/pet-cat-dog.svg" /><figcaption>
<p><span class="caption-number">Fig. 21 </span><span class="caption-text">UML class diagram showing the relationship between the classes <code class="docutils literal notranslate"><span class="pre">Pet</span></code>, <code class="docutils literal notranslate"><span class="pre">Dog</span></code>, and
<code class="docutils literal notranslate"><span class="pre">Cat</span></code>.</span><a class="headerlink" href="#fig-uml-pets-cat-dog-only" title="Permalink to this image">#</a></p>
</figcaption>
</figure>
</section>
</div><div class="proof example admonition" id="eg-inheritance-pets-cats-dogs-subclass-superclass">
<p class="admonition-title"><span class="caption-number">Example 55 </span> (Objects of the subclass are also objects of the superclass)</p>
<section class="example-content" id="proof-content">
<p>Consider the classes <code class="docutils literal notranslate"><span class="pre">Pet</span></code>, <code class="docutils literal notranslate"><span class="pre">Dog</span></code>, and <code class="docutils literal notranslate"><span class="pre">Cat</span></code> defined in
<a class="reference internal" href="#eg-inheritance-pets-cats-dogs">Example 54</a>. Since each object of the classes
<code class="docutils literal notranslate"><span class="pre">Dog</span></code> and <code class="docutils literal notranslate"><span class="pre">Cat</span></code> is also an object of the class <code class="docutils literal notranslate"><span class="pre">Pet</span></code>, we get a flexibility that
reminds us of what we achieved using <code class="docutils literal notranslate"><span class="pre">interface</span></code>s (as shown in
<a class="reference internal" href="interfaces.html#sec-interfaces-christmas-solution"><span class="std std-ref">Implementing the DTU Christmas Party Software Using interfaces</span></a>).</p>
<p>For instance, we can create objects of the classes <code class="docutils literal notranslate"><span class="pre">Dog</span></code> and <code class="docutils literal notranslate"><span class="pre">Cat</span></code>, and call the
method <code class="docutils literal notranslate"><span class="pre">getDescription()</span></code> provided by their superclass Pet:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">var</span><span class="w"> </span><span class="n">dog</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">Dog</span><span class="p">(</span><span class="s">"Mr Wolf"</span><span class="p">);</span>
<span class="kd">var</span><span class="w"> </span><span class="n">cat</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">Cat</span><span class="p">(</span><span class="s">"Ms Jones"</span><span class="p">);</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">"Dog's description: "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">dog</span><span class="p">.</span><span class="na">getDescription</span><span class="p">());</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">"Cat's description: "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">cat</span><span class="p">.</span><span class="na">getDescription</span><span class="p">());</span>
</pre></div>
</div>
<p>and the output of the code snippet above is:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Dog's description: Mr Wolf (dog)
Cat's description: Ms Jones (cat)
</pre></div>
</div>
<p>We can also put the <code class="docutils literal notranslate"><span class="pre">dog</span></code> and <code class="docutils literal notranslate"><span class="pre">cat</span></code> objects above in a generic array of <code class="docutils literal notranslate"><span class="pre">Pet</span></code>s
(since each object of the classes <code class="docutils literal notranslate"><span class="pre">Dog</span></code> and <code class="docutils literal notranslate"><span class="pre">Cat</span></code> also belongs to the superclass
<code class="docutils literal notranslate"><span class="pre">Pet</span></code>):</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">var</span><span class="w"> </span><span class="n">pets</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">Pet</span><span class="o">[]</span><span class="w"> </span><span class="p">{</span><span class="n">cat</span><span class="p">,</span><span class="w"> </span><span class="n">dog</span><span class="p">};</span>
</pre></div>
</div>
<p>And as expected, each object in the array <code class="docutils literal notranslate"><span class="pre">pets</span></code> lets us use the programming
interface (i.e. the <code class="docutils literal notranslate"><span class="pre">public</span></code> methods and fields) provided in the base class
<code class="docutils literal notranslate"><span class="pre">Pet</span></code>, e.g.:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></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">"Our pets are:"</span><span class="p">);</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">pets</span><span class="p">)</span><span class="w"> </span><span class="p">{</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">getDescription</span><span class="p">());</span>
<span class="p">}</span>
</pre></div>
</div>
<p>which produces the output:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Our pets are:
- Ms Jones (cat)
- Mr Wolf (dog)
</pre></div>
</div>
</section>
</div></section>
<section id="calling-a-super-constructor">
<span id="sec-inheritance-oo-inheritance-super-constructor"></span><span id="index-2"></span><h3>Calling a “<code class="docutils literal notranslate"><span class="pre">super</span></code>” Constructor<a class="headerlink" href="#calling-a-super-constructor" title="Permalink to this heading">#</a></h3>
<p>When a subclass extends a superclass, <strong>the constructors of the superclass are
<em>not</em> inherited by the subclass</strong>. Still, when writing the subclass, it is
sometimes handy to <strong>call a constructor of the superclass</strong> — for instance,
to reduce code duplication, and to initialise fields that are declared <code class="docutils literal notranslate"><span class="pre">private</span></code>
in the superclass. This is illustrated in
<a class="reference internal" href="#eg-inheritance-pets-cats-dogs-super">Example 56</a> and
<a class="reference internal" href="#eg-inheritance-pets-cats-dogs-super-private">Example 57</a> below.</p>
<div class="proof example admonition" id="eg-inheritance-pets-cats-dogs-super">
<p class="admonition-title"><span class="caption-number">Example 56 </span> (Using <code class="docutils literal notranslate"><span class="pre">super</span></code> to call the superclass constructor)</p>
<section class="example-content" id="proof-content">
<p>Consider the classes <code class="docutils literal notranslate"><span class="pre">Dog</span></code> and <code class="docutils literal notranslate"><span class="pre">Cat</span></code> in
<a class="reference internal" href="#eg-inheritance-pets-cats-dogs">Example 54</a>: they have some duplicated code in
their constructors, which are very similar. We can remove the duplication by
adding a generic constructor to the class <code class="docutils literal notranslate"><span class="pre">Pet</span></code> that takes as arguments the
pet’s <code class="docutils literal notranslate"><span class="pre">name</span></code> and <code class="docutils literal notranslate"><span class="pre">species</span></code>, and initialises the corresponding fields: (the
differences with <a class="reference internal" href="#eg-inheritance-pets-cats-dogs">Example 54</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">Pet</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">species</span><span class="p">;</span>
<span class="linenos"> 4</span>
<span class="hll"><span class="linenos"> 5</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="nf">Pet</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">species</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
</span><span class="hll"><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><span class="hll"><span class="linenos"> 7</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="na">species</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">species</span><span class="p">;</span>
</span><span class="hll"><span class="linenos"> 8</span><span class="w"> </span><span class="p">}</span>
</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">getDescription</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="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="s">" ("</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">species</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">")"</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>
</pre></div>
</div>
<p>We can then revise the classes <code class="docutils literal notranslate"><span class="pre">Dog</span></code> and <code class="docutils literal notranslate"><span class="pre">Cat</span></code> and make their constructors
simply call the constructor of their superclass <code class="docutils literal notranslate"><span class="pre">Pet</span></code>, passing the appropriate
arguments: (the differences with <a class="reference internal" href="#eg-inheritance-pets-cats-dogs">Example 54</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">Dog</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">Pet</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="nf">Dog</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="p">{</span>
<span class="hll"><span class="linenos"> 3</span><span class="w"> </span><span class="kd">super</span><span class="p">(</span><span class="n">name</span><span class="p">,</span><span class="w"> </span><span class="s">"dog"</span><span class="p">);</span>
</span><span class="linenos"> 4</span><span class="w"> </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">class</span> <span class="nc">Cat</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">Pet</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="nf">Cat</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="p">{</span>
<span class="hll"><span class="linenos"> 9</span><span class="w"> </span><span class="kd">super</span><span class="p">(</span><span class="n">name</span><span class="p">,</span><span class="w"> </span><span class="s">"cat"</span><span class="p">);</span>
</span><span class="linenos">10</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">11</span><span class="p">}</span>
</pre></div>
</div>
<p>After this change, the code duplication in <code class="docutils literal notranslate"><span class="pre">Dog</span></code> and <code class="docutils literal notranslate"><span class="pre">Cat</span></code> has been reduced, and
the code snippets shown in
<a class="reference internal" href="#eg-inheritance-pets-cats-dogs-subclass-superclass">Example 55</a> still work.
<em>(Try them!)</em></p>
<p>We can now also define objects of the class <code class="docutils literal notranslate"><span class="pre">Pet</span></code>, by using its constructor
directly:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">var</span><span class="w"> </span><span class="n">pet</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">Pet</span><span class="p">(</span><span class="s">"Wilbur"</span><span class="p">,</span><span class="w"> </span><span class="s">"parrot"</span><span class="p">);</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">"Pet's description: "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">pet</span><span class="p">.</span><span class="na">getDescription</span><span class="p">());</span>
</pre></div>
</div>
<p>and the code snippet above displays the output:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Pet's description: Wilbur (parrot)
</pre></div>
</div>
</section>
</div><div class="proof example admonition" id="eg-inheritance-pets-cats-dogs-super-private">
<p class="admonition-title"><span class="caption-number">Example 57 </span> (Making class fields private (when possible))</p>
<section class="example-content" id="proof-content">
<p>After the changes described in <a class="reference internal" href="#eg-inheritance-pets-cats-dogs-super">Example 56</a>,
we can make a further observation: the classes <code class="docutils literal notranslate"><span class="pre">Dog</span></code> and <code class="docutils literal notranslate"><span class="pre">Cat</span></code> are not accessing
the fields <code class="docutils literal notranslate"><span class="pre">name</span></code> and <code class="docutils literal notranslate"><span class="pre">species</span></code> of the class <code class="docutils literal notranslate"><span class="pre">Pet</span></code> any more (because the call to
the <code class="docutils literal notranslate"><span class="pre">super</span></code> constructor initialises them as needed). Therefore, we can <em>make
the fields <code class="docutils literal notranslate"><span class="pre">name</span></code> and <code class="docutils literal notranslate"><span class="pre">species</span></code> both <code class="docutils literal notranslate"><span class="pre">private</span></code> in the class <code class="docutils literal notranslate"><span class="pre">Pet</span></code></em>, without
changing the subclasses <code class="docutils literal notranslate"><span class="pre">Dog</span></code> and <code class="docutils literal notranslate"><span class="pre">Cat</span></code>, as follows: (the differences with the
class <code class="docutils literal notranslate"><span class="pre">Pet</span></code> in <a class="reference internal" href="#eg-inheritance-pets-cats-dogs-super">Example 56</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">Pet</span><span class="w"> </span><span class="p">{</span>
<span class="hll"><span class="linenos"> 2</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">name</span><span class="p">;</span>
</span><span class="hll"><span class="linenos"> 3</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">species</span><span class="p">;</span>
</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">Pet</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">species</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">species</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">species</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">getDescription</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="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="s">" ("</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">species</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">")"</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>
</pre></div>
</div>
<p>With this change, we have reduced the programming interface of the class <code class="docutils literal notranslate"><span class="pre">Pet</span></code>
(as it now only consists of the constructor and the method <code class="docutils literal notranslate"><span class="pre">getDescription()</span></code>),
and the code snippets shown in
<a class="reference internal" href="#eg-inheritance-pets-cats-dogs-subclass-superclass">Example 55</a> still work. <em>(Try
them!)</em></p>
<p>Also, you will notice that if you modify the classes <code class="docutils literal notranslate"><span class="pre">Dog</span></code> or <code class="docutils literal notranslate"><span class="pre">Cat</span></code> and add a
method that tries to access the <code class="docutils literal notranslate"><span class="pre">private</span></code> fields <code class="docutils literal notranslate"><span class="pre">this.name</span></code> or
<code class="docutils literal notranslate"><span class="pre">this.description</span></code>, you will get an error, because those fields are not visible
in the subclass. <em>(Try it!)</em></p>
</section>
</div><div class="admonition note">
<p class="admonition-title">Note</p>
<p>Making class fields and methods <code class="docutils literal notranslate"><span class="pre">private</span></code> whenever possible (as shown at the end
of <a class="reference internal" href="#eg-inheritance-pets-cats-dogs-super">Example 56</a>) is generally good practice:
this minimises the programming interfaces of classes and objects, and makes
software easier to maintain. This is because as software evolves, changing a
<code class="docutils literal notranslate"><span class="pre">private</span></code> field or method will impact the owner class, but will not require
modifying other parts of our programs.</p>
</div>
<span class="target" id="index-3"></span></section>
<section id="overriding-methods">
<span id="sec-inheritance-oo-inheritance-overriding-methods"></span><span id="index-4"></span><h3>Overriding Methods<a class="headerlink" href="#overriding-methods" title="Permalink to this heading">#</a></h3>
<p>It is sometimes handy for a subclass to <strong>override</strong> (i.e., redefine) a method
that is already provided by a superclass, to make it “more specialised” for the
subclass. When this happens, the method in the subclass has “priority” over the
original one — as shown in <a class="reference internal" href="#eg-inheritance-pets-snake-override">Example 58</a>
below.</p>
<div class="proof example admonition" id="eg-inheritance-pets-snake-override">
<p class="admonition-title"><span class="caption-number">Example 58 </span> (Overriding a method in a subclass)</p>
<section class="example-content" id="proof-content">
<p>Consider again the class <code class="docutils literal notranslate"><span class="pre">Pet</span></code> in
<a class="reference internal" href="#eg-inheritance-pets-cats-dogs-super-private">Example 57</a>, and suppose that we need
to define a derived class <code class="docutils literal notranslate"><span class="pre">Snake</span></code> to represent a pet snake, with information on
whether the snake is poisonous or not. Also, suppose we want the method
<code class="docutils literal notranslate"><span class="pre">getDescription()</span></code> of <code class="docutils literal notranslate"><span class="pre">Snake</span></code> objects to report whether the snake is poisonous
or not.</p>
<p>This last requirement means that we cannot reuse the method <code class="docutils literal notranslate"><span class="pre">getDescription()</span></code>
provided by the class <code class="docutils literal notranslate"><span class="pre">Pet</span></code>. However, we can <em>override</em> that method in the
class <code class="docutils literal notranslate"><span class="pre">Snake</span></code>, as follows:</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">Snake</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">Pet</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 2</span><span class="w"> </span><span class="kd">private</span><span class="w"> </span><span class="kt">boolean</span><span class="w"> </span><span class="n">isPoisonous</span><span class="p">;</span>
<span class="linenos"> 3</span>
<span class="linenos"> 4</span><span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="nf">Snake</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">boolean</span><span class="w"> </span><span class="n">isPoisonous</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="linenos"> 5</span><span class="w"> </span><span class="kd">super</span><span class="p">(</span><span class="n">name</span><span class="p">,</span><span class="w"> </span><span class="s">"snake"</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">isPoisonous</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">isPoisonous</span><span class="p">;</span>
<span class="linenos"> 7</span><span class="w"> </span><span class="p">}</span>
<span class="linenos"> 8</span>
<span class="linenos"> 9</span><span class="w"> </span><span class="nd">@Override</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">getDescription</span><span class="p">()</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">danger</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">isPoisonous</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="s">"DANGER: poisonous"</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="s">"non-poisonous"</span><span class="p">;</span>
<span class="linenos">12</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="kd">super</span><span class="p">.</span><span class="na">getDescription</span><span class="p">()</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">danger</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">")"</span><span class="p">;</span>
<span class="linenos">13</span><span class="w"> </span><span class="p">}</span>
<span class="linenos">14</span><span class="p">}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<ul class="simple">
<li><p>On line 12, we directly call the method <code class="docutils literal notranslate"><span class="pre">getDescription()</span></code> provided
by the superclass <code class="docutils literal notranslate"><span class="pre">Pet</span></code>, using <code class="docutils literal notranslate"><span class="pre">super</span></code> to access it.</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">@Override</span></code> annotation on line 9 is not mandatory, but it is good practice
to write it, because it lets us spot at a glance whether a method overrides
another method in a superclass.</p></li>
</ul>
</div>
<p>We can now create objects of the class <code class="docutils literal notranslate"><span class="pre">Snake</span></code>, and we can observe that <strong>if we
call their method <code class="docutils literal notranslate"><span class="pre">getDescription()</span></code>, the “more specialised” version defined in
<code class="docutils literal notranslate"><span class="pre">Snake</span></code> is executed</strong>:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">var</span><span class="w"> </span><span class="n">snake1</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">Snake</span><span class="p">(</span><span class="s">"Crawley"</span><span class="p">,</span><span class="w"> </span><span class="kc">false</span><span class="p">);</span>
<span class="kd">var</span><span class="w"> </span><span class="n">snake2</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">Snake</span><span class="p">(</span><span class="s">"Mambojumbo"</span><span class="p">,</span><span class="w"> </span><span class="kc">true</span><span class="p">);</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">"First snake description: "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">snake1</span><span class="p">.</span><span class="na">getDescription</span><span class="p">());</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">"Second snake description: "</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">snake2</span><span class="p">.</span><span class="na">getDescription</span><span class="p">());</span>
</pre></div>
</div>
<p>and the output of the code snippet above is:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>First snake description: Crawley (snake) (non-poisonous)
Second snake description: Mambojumbo (snake) (DANGER: poisonous)
</pre></div>
</div>
<p>We can also observe the “more specialised” method <code class="docutils literal notranslate"><span class="pre">getDescription()</span></code> being used,
e.g., if we create an array of <code class="docutils literal notranslate"><span class="pre">Pet</span></code>s including <code class="docutils literal notranslate"><span class="pre">snake1</span></code> and <code class="docutils literal notranslate"><span class="pre">snake2</span></code> defined
above, and then call <code class="docutils literal notranslate"><span class="pre">getDescription()</span></code> on each object in the array.</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">var</span><span class="w"> </span><span class="n">dog</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">Dog</span><span class="p">(</span><span class="s">"Mr Wolf"</span><span class="p">);</span>
<span class="kd">var</span><span class="w"> </span><span class="n">cat</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">Cat</span><span class="p">(</span><span class="s">"Ms Jones"</span><span class="p">);</span>
<span class="kd">var</span><span class="w"> </span><span class="n">pets</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">Pet</span><span class="o">[]</span><span class="w"> </span><span class="p">{</span><span class="n">cat</span><span class="p">,</span><span class="w"> </span><span class="n">dog</span><span class="p">,</span><span class="w"> </span><span class="n">snake1</span><span class="p">,</span><span class="w"> </span><span class="n">snake2</span><span class="p">};</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">"Our pets are:"</span><span class="p">);</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">pets</span><span class="p">)</span><span class="w"> </span><span class="p">{</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">getDescription</span><span class="p">());</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The code above will print on the console:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Our pets are:
- Ms Jones (cat)
- Mr Wolf (dog)
- Crawley (snake) (non-poisonous)
- Mambojumbo (snake) (DANGER: poisonous)
</pre></div>
</div>
<p>Observe that the code above calls <code class="docutils literal notranslate"><span class="pre">p.getDescription()</span></code> using a loop variable <code class="docutils literal notranslate"><span class="pre">p</span></code>
of type <code class="docutils literal notranslate"><span class="pre">Pet</span></code> — and when <code class="docutils literal notranslate"><span class="pre">p</span></code> refers to an object of the subclass <code class="docutils literal notranslate"><span class="pre">Snake</span></code>, then
the “more specialised” method <code class="docutils literal notranslate"><span class="pre">getDescription()</span></code> provided by the class <code class="docutils literal notranslate"><span class="pre">Snake</span></code>
is executed.</p>
</section>
</div></section>
</section>
<section id="what-is-object-oriented-programming">
<span id="sec-inheritance-oo-oo"></span><h2>What is “Object-Oriented Programming”?<a class="headerlink" href="#what-is-object-oriented-programming" title="Permalink to this heading">#</a></h2>
<p>You may find various (more or less similar) definitions of “object-oriented
programming” — so, the definition below tries to be a general enough, without
delving into excessive details.</p>
<p>Object-oriented programming is a programming approach where programs are
designed and developed using <strong>objects</strong> as the main building block; objects
provide <strong>fields</strong> (a.k.a. <strong>attributes</strong>) to store data, and <strong>methods</strong> to
read and manipulate that data; the shape of each object (i.e. which fields and
methods it provides) is determined by the <strong>class</strong> of the object.</p>
<p>We have already seen and used all the elements above — but the object-oriented
approach also encourages the use of the following code design techniques.</p>
<ul>
<li><p>Leverage <strong><a class="reference internal" href="prog-interf-encapsulation.html#sec-interfaces-encapsulation-encapsulation"><span class="std std-ref">encapsulation</span></a></strong>,
i.e. minimise the programming interface of objects by making their fields and
methods <code class="docutils literal notranslate"><span class="pre">private</span></code> (whenever possible).</p></li>
<li><p>Leverage <strong>abstraction</strong>, i.e. design code that targets generic (abstract)
programming interfaces. We have already done this, by:</p>
<ul class="simple">
<li><p><a class="reference internal" href="interfaces.html#sec-interfaces-interfaces"><span class="std std-ref">defining interfaces and writing code that only depends on them</span></a>, and</p></li>
<li><p>writing code that that only depends on a superclass (not on its specific
subclasses), like the loops on arrays of generic <code class="docutils literal notranslate"><span class="pre">Pet</span></code>s in
<a class="reference internal" href="#eg-inheritance-pets-cats-dogs-subclass-superclass">Example 55</a> and
<a class="reference internal" href="#eg-inheritance-pets-snake-override">Example 58</a>.</p></li>
</ul>
</li>
<li><p>Leverage <strong>inheritance</strong>, i.e. reuse parts of a superclass in one or more subclasses,
as described <a class="reference internal" href="#sec-inheritance-oo-inheritance"><span class="std std-ref">above</span></a>.</p></li>
<li><p>Leverage <strong>subtype polymorphism</strong>, i.e. design generic code that manipulates
objects without depending on their specific classes — and yet, let the code
be <em>“polymorphic”</em> (i.e. have different behaviours) depending on the actual
object being manipulated. We have already seen subtype polymorphism in
action, e.g.:</p>
<ul class="simple">
<li><p>in <a class="reference internal" href="interfaces.html#eg-multiple-interfaces">Example 53</a>, you can observe that our
code calls the method <code class="docutils literal notranslate"><span class="pre">.getDescription()</span></code> on each object contained in an array
of generic <code class="docutils literal notranslate"><span class="pre">Device</span></code>s, and the actual method being executed may vary (since the
method may be provided by an object of the class <code class="docutils literal notranslate"><span class="pre">Scanner</span></code>, <code class="docutils literal notranslate"><span class="pre">Printer</span></code>, or
<code class="docutils literal notranslate"><span class="pre">AllInOne</span></code>);</p></li>
<li><p>similarly, in <a class="reference internal" href="#eg-inheritance-pets-snake-override">Example 58</a>, you can observe
a loop that calls the method <code class="docutils literal notranslate"><span class="pre">.getDescription()</span></code> on each object contained in
an array of generic <code class="docutils literal notranslate"><span class="pre">Pet</span></code>s, and the actual method being executed may vary
(since the method may be provided by the class <code class="docutils literal notranslate"><span class="pre">Pet</span></code> or <code class="docutils literal notranslate"><span class="pre">Snake</span></code>).</p></li>
</ul>
<p>(We will reprise the topic of subtype polymorphism in more detail later in the
course.)</p>
</li>
</ul>
</section>
<section id="concluding-remarks">
<span id="sec-inheritance-oo-concluding-remarks"></span><h2>Concluding Remarks<a class="headerlink" href="#concluding-remarks" title="Permalink to this heading">#</a></h2>
<p>You should now have an understanding of two main topics:</p>
<ul class="simple">
<li><p>What is <a class="reference internal" href="#sec-inheritance-oo-inheritance"><span class="std std-ref">class inheritance</span></a> and
how to use it, to improve the structure and the code reuse in our programs</p></li>
<li><p>What is <a class="reference internal" href="#sec-inheritance-oo-oo"><span class="std std-ref">object-oriented programming</span></a> and
what are its main principles.</p></li>
</ul>
<p>It takes some practice to master the use of inheritance — and, more generally,
to design software that satisfies the principles of object-oriented programming.
Moreover, there are often multiple ways to design and implement a program that
satisfies some given requirements.</p>
<p>The more you program, the more you will gain experience and skills to master all
these aspects.</p>
</section>
<section id="references-and-further-readings">
<span id="sec-inheritance-oo-references-readings"></span><h2>References and Further Readings<a class="headerlink" href="#references-and-further-readings" title="Permalink to this heading">#</a></h2>
<p>You are invited to read the following sections of the
<a class="reference internal" href="overview.html#sec-book"><span class="std std-ref">reference book</span></a>: (Note: they might mention Java features
that we will address later in the course)</p>
<ul class="simple">
<li><p>Section 9.1 - “Creating Subclasses”</p></li>
<li><p>Section 9.2 - “Overriding Methods”</p></li>
<li><p>Section 9.3 - “Class Hierarchies”</p></li>
<li><p>Section 9.4 - “Visibility”</p></li>
</ul>
</section>
<section id="lab-and-weekly-assessments">
<span id="sec-inheritance-oo-lab"></span><h2>Lab and Weekly Assessments<a class="headerlink" href="#lab-and-weekly-assessments" title="Permalink to this heading">#</a></h2>
<p>During the lab you can begin your work on the <strong>weekly assessments</strong> below.</p>
<ul class="simple">
<li><p><a class="reference internal" href="#lab-inheritance-oo-vending-machines-2"><span class="std std-ref">06 - Vending Machines, Version 2</span></a></p></li>
<li><p><a class="reference internal" href="#lab-inheritance-oo-vehicles2"><span class="std std-ref">07 - Vehicle Fleet Management, Version 2</span></a></p></li>
<li><p><a class="reference internal" href="#lab-inheritance-oo-pets"><span class="std std-ref">08 - Pets</span></a></p></li>
</ul>
<div class="admonition important">
<p class="admonition-title">Important</p>
<ul class="simple">
<li><p>For each assessment, you can download the corresponding handout and submit
your solution on <strong>DTU Autolab: <a title="Autolab assessments" href="https://autolab.compute.dtu.dk/courses/02312-E24">https://autolab.compute.dtu.dk/courses/02312-E24</a></strong>.</p></li>
<li><p>For details on how to use Autolab and the assessment handouts, and how to
submit your solutions, please read <a class="reference internal" href="overview.html#sec-autolab"><span class="std std-ref">these instructions</span></a>.</p></li>
<li><p>If you have troubles, you can
<a class="reference internal" href="overview.html#sec-help"><span class="std std-ref">get help from the teacher and TAs</span></a>.</p></li>
</ul>
</div>
<section id="vending-machines-version-2">
<span id="lab-inheritance-oo-vending-machines-2"></span><h3>06 - Vending Machines, Version 2<a class="headerlink" href="#vending-machines-version-2" title="Permalink to this heading">#</a></h3>
<p>This is a follow-up to the assessment
<a class="reference internal" href="prog-interf-encapsulation.html#lab-interfaces-encapsulation-vending-machines"><span class="std std-ref">03 - Vending Machines</span></a>. Your task is to edit
the file <code class="docutils literal notranslate"><span class="pre">VendingMachine.java</span></code> (which you can take from the solution to
<a class="reference internal" href="prog-interf-encapsulation.html#lab-interfaces-encapsulation-vending-machines"><span class="std std-ref">03 - Vending Machines</span></a>) and implement a
new class called <code class="docutils literal notranslate"><span class="pre">AdvancedVendingMachine</span></code> that extends the class
<code class="docutils literal notranslate"><span class="pre">VendingMachine</span></code>.</p>
<p>The new class <code class="docutils literal notranslate"><span class="pre">AdvancedVendingMachine</span></code> can only have <code class="docutils literal notranslate"><span class="pre">private</span></code> fields, and it
must inherit <em>all</em> the methods of <code class="docutils literal notranslate"><span class="pre">VendingMachine</span></code>. In addition, objects of the
class <code class="docutils literal notranslate"><span class="pre">AdvancedVendingMachine</span></code> must satisfy the following requirements.</p>
<ul>
<li><p>The class <code class="docutils literal notranslate"><span class="pre">AdvancedVendingMachine</span></code> must have a <code class="docutils literal notranslate"><span class="pre">public</span></code> constructor that takes
the machines’s location (as a <code class="docutils literal notranslate"><span class="pre">String</span></code>) and the number of coffee and chocolate
servings it holds (as <code class="docutils literal notranslate"><span class="pre">int</span></code>egers). A newly-created vending machine must hold
no cash. For instance, the constructor should allow us to create a vending
machine object with 10 servings of coffee and 12 servings of chocolate, as
follows:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">var</span><span class="w"> </span><span class="n">m</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">AdvancedVendingMachine</span><span class="p">(</span><span class="s">"DTU Building 324"</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">,</span><span class="w"> </span><span class="mi">12</span><span class="p">);</span>
</pre></div>
</div>
</li>
<li><p><code class="docutils literal notranslate"><span class="pre">AdvancedVendingMachine</span></code> objects must have the following method:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">serveCoffees</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">serveChocolates</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">serveWienerMelanges</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span>
</pre></div>
</div>
<p>These methods attempt to provide <code class="docutils literal notranslate"><span class="pre">n</span></code> servings of the requested beverage, and
return the number of servings are actually provided. These methods must follow
the same principles of the inherited methods <code class="docutils literal notranslate"><span class="pre">serveCoffee()</span></code>,
<code class="docutils literal notranslate"><span class="pre">serveChocolate()</span></code>, and <code class="docutils literal notranslate"><span class="pre">serveWienerMelange()</span></code>. For instance, if the advanced
vending machine <code class="docutils literal notranslate"><span class="pre">m</span></code> holds 2 servings of coffee and 30 units of cash, then the
method call <code class="docutils literal notranslate"><span class="pre">m.serveCoffees(5)</span></code> must return 2, because the vending machine
runs out of coffee after providing 2 servings; after the method call, the
vending machine <code class="docutils literal notranslate"><span class="pre">m</span></code> must have 10 units of cash left.</p>
</li>
</ul>
<p>When you are done, submit the modified file <code class="docutils literal notranslate"><span class="pre">VendingMachine.java</span></code> on DTU Autolab.</p>
<div class="admonition tip">
<p class="admonition-title">Tip</p>
<ul class="simple">
<li><p>The constructor of <code class="docutils literal notranslate"><span class="pre">AdvancedVendingMachine</span></code> can be easily implemented by
<a class="reference internal" href="#sec-inheritance-oo-inheritance-super-constructor"><span class="std std-ref">calling the “super” constructor</span></a>…</p></li>
<li><p>The 3 methods of <code class="docutils literal notranslate"><span class="pre">AdvancedVendingMachine</span></code> can be implemented by reusing the
methods of the parent class <code class="docutils literal notranslate"><span class="pre">VendingMachine</span></code>.</p></li>
<li><p>This assignment can be solved in various ways — but there is a simple
solution where the class <code class="docutils literal notranslate"><span class="pre">AdvancedVendingMachine</span></code> does not have any field.</p></li>
</ul>
</div>
</section>
<section id="vehicle-fleet-management-version-2">
<span id="lab-inheritance-oo-vehicles2"></span><h3>07 - Vehicle Fleet Management, Version 2<a class="headerlink" href="#vehicle-fleet-management-version-2" title="Permalink to this heading">#</a></h3>
<p>This is a follow-up to the assessment
<a class="reference internal" href="interfaces.html#lab-interfaces-vehicles"><span class="std std-ref">04 - Vehicle Fleet Management</span></a>: the goal is to turn <code class="docutils literal notranslate"><span class="pre">Vehicle</span></code> into
a <code class="docutils literal notranslate"><span class="pre">class</span></code> (instead of being an <code class="docutils literal notranslate"><span class="pre">interface</span></code>) that contains reusable
methods (and minimises code duplication).</p>
<p>You can choose to start this exercise from scratch, or you can edit the file
<code class="docutils literal notranslate"><span class="pre">Vehicle.java</span></code> that solves <a class="reference internal" href="interfaces.html#lab-interfaces-vehicles"><span class="std std-ref">04 - Vehicle Fleet Management</span></a> (either
your own file, or the one provided by the teacher as a solution to
<a class="reference internal" href="interfaces.html#lab-interfaces-vehicles"><span class="std std-ref">04 - Vehicle Fleet Management</span></a>).</p>
<p>Your task is to write in the file <code class="docutils literal notranslate"><span class="pre">Vehicle.java</span></code> the classes that correspond to
the class diagram in <a class="reference internal" href="#fig-uml-vehicles-inheritance"><span class="std std-numref">Fig. 22</span></a> below. Note that:</p>
<ul class="simple">
<li><p>the class <code class="docutils literal notranslate"><span class="pre">Vehicle</span></code> must implement both methods <code class="docutils literal notranslate"><span class="pre">getModel()</span></code> and
<code class="docutils literal notranslate"><span class="pre">getRegistrationPlate()</span></code>, and only have <code class="docutils literal notranslate"><span class="pre">private</span></code> fields; and</p></li>
<li><p>the classes <code class="docutils literal notranslate"><span class="pre">Minivan</span></code> and <code class="docutils literal notranslate"><span class="pre">Truck</span></code> must <em>not</em> reimplement (i.e. <a class="reference internal" href="#sec-inheritance-oo-inheritance-overriding-methods"><span class="std std-ref">override</span></a>) the methods
<code class="docutils literal notranslate"><span class="pre">getModel()</span></code> and <code class="docutils literal notranslate"><span class="pre">getRegistrationPlate()</span></code>. Instead, the classes <code class="docutils literal notranslate"><span class="pre">Minivan</span></code> and
<code class="docutils literal notranslate"><span class="pre">Truck</span></code> must rely on the methods <code class="docutils literal notranslate"><span class="pre">getModel()</span></code> and <code class="docutils literal notranslate"><span class="pre">getRegistrationPlate()</span></code>
provided by their superclass <code class="docutils literal notranslate"><span class="pre">Vehicle</span></code> (to minimise code duplication).</p></li>
</ul>
<figure class="align-default" id="fig-uml-vehicles-inheritance">
<img alt="UML class diagram for "Vehicle fleet management, version 2" assessment" src="_images/vehicle-nonabstract-inheritance.svg" /><figcaption>
<p><span class="caption-number">Fig. 22 </span><span class="caption-text">UML diagram for the revised classes in <a class="reference internal" href="#lab-inheritance-oo-vehicles2"><span class="std std-ref">07 - Vehicle Fleet Management, Version 2</span></a>.</span><a class="headerlink" href="#fig-uml-vehicles-inheritance" title="Permalink to this image">#</a></p>
</figcaption>
</figure>
<p>When you are done, submit the modified file <code class="docutils literal notranslate"><span class="pre">Vehicle.java</span></code> on DTU Autolab.</p>
<p>The handout includes some Java files called <code class="docutils literal notranslate"><span class="pre">Test01.java</span></code>, <code class="docutils literal notranslate"><span class="pre">Test02.java</span></code>, etc.:
they are test programs that use the code you should write in <code class="docutils literal notranslate"><span class="pre">Vehicle.java</span></code>,
and they might not compile or work correctly until you complete your work. You
should read those test programs, try to run them, and also run <code class="docutils literal notranslate"><span class="pre">./grade</span></code> to see
their expected outputs — but you must <em>not</em> modify those files.</p>
<div class="admonition hint">
<p class="admonition-title">Hint</p>
<p>You may want to store the model and registration plate in <code class="docutils literal notranslate"><span class="pre">private</span></code> fields
of the class <code class="docutils literal notranslate"><span class="pre">Vehicle</span></code>, that you can initialise using <code class="docutils literal notranslate"><span class="pre">Vehicle</span></code>’s constructor
(which you can call from the subclasses using <code class="docutils literal notranslate"><span class="pre">super</span></code>,
as shown in <a class="reference internal" href="#sec-inheritance-oo-inheritance-super-constructor"><span class="std std-ref">Calling a “super” Constructor</span></a>).</p>
</div>
</section>
<section id="pets">
<span id="lab-inheritance-oo-pets"></span><h3>08 - Pets<a class="headerlink" href="#pets" title="Permalink to this heading">#</a></h3>
<p>This assignment builds upon <a class="reference internal" href="#eg-inheritance-pets-cats-dogs">Example 54</a> and
<a class="reference internal" href="#eg-inheritance-pets-cats-dogs-super">Example 56</a>, and you can reuse and adapt some
code from there. Your task is to edit the file <code class="docutils literal notranslate"><span class="pre">Pet.java</span></code> provided in the
handout and implement the class hierarchy shown in
<a class="reference internal" href="#fig-uml-pets-cat-dog-assignment"><span class="std std-numref">Fig. 23</span></a> below.</p>
<figure class="align-default" id="fig-uml-pets-cat-dog-assignment">
<img alt="UML class diagram for the pet classes in the assignment "08 - Pets"" src="_images/pet-cat-dog-assignment.svg" /><figcaption>
<p><span class="caption-number">Fig. 23 </span><span class="caption-text">UML class diagram showing the relationship between the classes <code class="docutils literal notranslate"><span class="pre">Pet</span></code>, <code class="docutils literal notranslate"><span class="pre">Dog</span></code>,
<code class="docutils literal notranslate"><span class="pre">Cat</span></code>, <code class="docutils literal notranslate"><span class="pre">Frog</span></code>, and their subclasses.</span><a class="headerlink" href="#fig-uml-pets-cat-dog-assignment" title="Permalink to this image">#</a></p>
</figcaption>
</figure>
<p>Here are the requirements for the <code class="docutils literal notranslate"><span class="pre">Pet</span></code> class hierarchy. (Observe that, compared
to <a class="reference internal" href="#eg-inheritance-pets-cats-dogs">Example 54</a> and
<a class="reference internal" href="#eg-inheritance-pets-cats-dogs-super">Example 56</a>, there are several differences.)</p>
<ul>
<li><p>The class <code class="docutils literal notranslate"><span class="pre">Pet</span></code> has a constructor that takes as arguments the name and species
of the pet.</p></li>
<li><p>All the fields of all classes must be <code class="docutils literal notranslate"><span class="pre">private</span></code> (correspondingly, the UML
diagram in <a class="reference internal" href="#fig-uml-pets-cat-dog-assignment"><span class="std std-numref">Fig. 23</span></a> does not show any field).</p></li>
<li><p>The class <code class="docutils literal notranslate"><span class="pre">Pet</span></code> must implement the method <code class="docutils literal notranslate"><span class="pre">getDescription()</span></code>, and that method
must be <em>inherited</em> and reused by all subclasses (i.e., the method must <em>not</em>
be overridden by any subclass of <code class="docutils literal notranslate"><span class="pre">Pet</span></code>). The method <code class="docutils literal notranslate"><span class="pre">getDescription()</span></code> must
return a string describing the name and species of the pet. E.g., if a <code class="docutils literal notranslate"><span class="pre">Pet</span></code>
object <code class="docutils literal notranslate"><span class="pre">p</span></code> has name <code class="docutils literal notranslate"><span class="pre">"Pernille"</span></code> and species <code class="docutils literal notranslate"><span class="pre">"capibara"</span></code>, then
<code class="docutils literal notranslate"><span class="pre">p.getDescription()</span></code> must return the string:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Pernille (capibara)
</pre></div>
</div>
</li>
<li><p>The classes that directly inherit from <code class="docutils literal notranslate"><span class="pre">Pet</span></code> also determine the species of the
resulting object. More specifically:</p>
<ul>
<li><p>The species of an object of the class <code class="docutils literal notranslate"><span class="pre">Dog</span></code> is <code class="docutils literal notranslate"><span class="pre">"dog"</span></code> — and this also
applies to objects of all subclasses of <code class="docutils literal notranslate"><span class="pre">Dog</span></code>. For example, suppose we have
an object of the class <code class="docutils literal notranslate"><span class="pre">Chihuahua</span></code> created as follows:</p>
<div class="highlight-java notranslate"><div class="highlight"><pre><span></span><span class="kd">var</span><span class="w"> </span><span class="n">c</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">Chihuahua</span><span class="p">(</span><span class="s">"Prosit"</span><span class="p">)</span>
</pre></div>
</div>
<p>Then, <code class="docutils literal notranslate"><span class="pre">c.getDescription()</span></code> must return the string:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Prosit (dog)`
</pre></div>
</div>
</li>
<li><p>The species of an object of the class <code class="docutils literal notranslate"><span class="pre">Cat</span></code> is <code class="docutils literal notranslate"><span class="pre">"cat"</span></code> — and this also
applies to objects of all subclasses of <code class="docutils literal notranslate"><span class="pre">Cat</span></code>.</p></li>
<li><p>The species of an object of the class <code class="docutils literal notranslate"><span class="pre">Frog</span></code> is <code class="docutils literal notranslate"><span class="pre">"frog"</span></code> — and this also
applies to objects of all subclasses of <code class="docutils literal notranslate"><span class="pre">Frog</span></code>.</p></li>
</ul>
</li>
<li><p>Each class must implement its own version of the method <code class="docutils literal notranslate"><span class="pre">getSound()</span></code>, which
must return a string according to the following table. (In other words, each
class must <a class="reference internal" href="#sec-inheritance-oo-inheritance-overriding-methods"><span class="std std-ref">override</span></a>
the method <code class="docutils literal notranslate"><span class="pre">getSound()</span></code> of its superclass.)</p>
<table class="table" id="table-animal-sounds-assignment">
<caption><span class="caption-number">Table 9 </span><span class="caption-text">Animal sounds</span><a class="headerlink" href="#table-animal-sounds-assignment" title="Permalink to this table">#</a></caption>
<thead>
<tr class="row-odd"><th class="head"><p>Pet class</p></th>
<th class="head"><p>Sound</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">Pet</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">"?"</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">Dog</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">"Woof!"</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">Greyhound</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">"WOOF!!!"</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">Chihuahua</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">"Arf!"</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">Cat</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">"Meow!"</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">NorwegianForestCat</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">"MEOOOW!!!"</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">Frog</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">"Ribbit!"</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">HornedFrog</span></code></p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">"Croak!"</span></code></p></td>
</tr>
</tbody>
</table>
</li>
</ul>
<p>When you are done, submit the modified file <code class="docutils literal notranslate"><span class="pre">Pet.java</span></code> on DTU Autolab.</p>
<p>The handout includes some Java files called <code class="docutils literal notranslate"><span class="pre">Test01.java</span></code>, <code class="docutils literal notranslate"><span class="pre">Test02.java</span></code>, etc.:
they are test programs that use the code you should write in <code class="docutils literal notranslate"><span class="pre">Pet.java</span></code>,
and they might not compile or work correctly until you complete your work. You
should read those test programs, try to run them, and also run <code class="docutils literal notranslate"><span class="pre">./grade</span></code> to see
their expected outputs — but you must <em>not</em> modify those files.</p>
<div class="admonition hint">
<p class="admonition-title">Hint</p>
<ul class="simple">
<li><p>You may want to store the name and species of a pet in <code class="docutils literal notranslate"><span class="pre">private</span></code> fields
of the class <code class="docutils literal notranslate"><span class="pre">Pet</span></code>, that you can initialise using <code class="docutils literal notranslate"><span class="pre">Pet</span></code>’s constructor
(which you can call from the subclasses using <code class="docutils literal notranslate"><span class="pre">super</span></code>,
as shown in <a class="reference internal" href="#sec-inheritance-oo-inheritance-super-constructor"><span class="std std-ref">Calling a “super” Constructor</span></a>).</p></li>
<li><p>In the constructors of the classes in the lower row of
<a class="reference internal" href="#fig-uml-pets-cat-dog-assignment"><span class="std std-numref">Fig. 23</span></a>, you may want to call the <code class="docutils literal notranslate"><span class="pre">super</span></code>
constructor to initialise the pet’s name and species…</p></li>
</ul>
</div>
</section>
</section>
</section>
</article>
<footer class="bd-footer-article">
<div class="footer-article-items footer-article__inner">
<div class="footer-article-item"><!-- Previous / next buttons -->
<div class="prev-next-area">
<a class="left-prev"
href="lab-day-4.html"
title="previous page">
<i class="fa-solid fa-angle-left"></i>
<div class="prev-next-info">
<p class="prev-next-subtitle">previous</p>
<p class="prev-next-title">Module 9, Part 1: Lab Day</p>
</div>
</a>
<a class="right-next"
href="inheritance-2.html"
title="next page">
<div class="prev-next-info">
<p class="prev-next-subtitle">next</p>
<p class="prev-next-title">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</p>
</div>
<i class="fa-solid fa-angle-right"></i>
</a>
</div></div>
</div>
</footer>
</div>
<div class="bd-sidebar-secondary bd-toc"><div class="sidebar-secondary-items sidebar-secondary__inner">
<div class="sidebar-secondary-item">
<div class="page-toc tocsection onthispage">
<i class="fa-solid fa-list"></i> Contents
</div>