-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritance.html
More file actions
658 lines (551 loc) · 43.4 KB
/
Inheritance.html
File metadata and controls
658 lines (551 loc) · 43.4 KB
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
<!DOCTYPE html>
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>The Swift Programming Language: Inheritance</title>
<link rel="stylesheet" type="text/css" href="resource/style-1.1.15.css">
<meta charset="utf-8"> <script>window["_GOOG_TRANS_EXT_VER"] = "1";</script></head>
<body id="conceptual_flow_with_tasks" class="jazz">
<div id="_omniture_top">
</div>
<a name="TP40014097" title="The Swift Programming Language"></a>
<section id="valence">
<div class="content-wrapper">
<p id="hierarchial_navigation">
<span id="book_title">The Swift Programming Language</span>
</p>
<img id="shortstack" src="./resource/shortstack_2x.png">
</div>
</section>
<div class="content-wrapper">
<nav class="book-parts hideInXcode" role="navigation">
<ul class="nav-parts">
<li data-id="TP40014097-CH1-XID_27" class="part-name">Welcome to Swift
<ul class="nav-chapters">
<li class="nav-chapter nav-visited-chapter">
<a href="About Swift.html#TP40014097-CH3-XID_0" data-id="TP40014097-CH3-XID_0" class="">About Swift</a>
</li>
<li class="nav-chapter">
<a href="A Swift Tour.html#TP40014097-CH2-XID_1" data-id="TP40014097-CH2-XID_1" class="">A Swift Tour</a>
</li>
</ul>
</li><li data-id="TP40014097-CH4-XID_299" class="part-name nav-part-active open-part">Language Guide
<ul class="nav-chapters" style="height: 607px;">
<li class="nav-chapter">
<a href="The Basics.html#TP40014097-CH5-XID_399" data-id="TP40014097-CH5-XID_399" class="">The Basics</a>
</li>
<li class="nav-chapter">
<a href="Basic Operators.html#TP40014097-CH6-XID_70" data-id="TP40014097-CH6-XID_70" class="">Basic Operators</a>
</li>
<li class="nav-chapter">
<a href="Strings and Characters.html#TP40014097-CH7-XID_368" data-id="TP40014097-CH7-XID_368" class="">Strings and Characters</a>
</li>
<li class="nav-chapter">
<a href="Collection Types.html#TP40014097-CH8-XID_133" data-id="TP40014097-CH8-XID_133" class="">Collection Types</a>
</li>
<li class="nav-chapter">
<a href="Control Flow.html#TP40014097-CH9-XID_153" data-id="TP40014097-CH9-XID_153" class="">Control Flow</a>
</li>
<li class="nav-chapter">
<a href="Functions.html#TP40014097-CH10-XID_204" data-id="TP40014097-CH10-XID_204" class="">Functions</a>
</li>
<li class="nav-chapter">
<a href="Closures.html#TP40014097-CH11-XID_117" data-id="TP40014097-CH11-XID_117" class="">Closures</a>
</li>
<li class="nav-chapter">
<a href="Enumerations.html#TP40014097-CH12-XID_185" data-id="TP40014097-CH12-XID_185" class="">Enumerations</a>
</li>
<li class="nav-chapter">
<a href="Classes and Structures.html#TP40014097-CH13-XID_94" data-id="TP40014097-CH13-XID_94" class="">Classes and Structures</a>
</li>
<li class="nav-chapter">
<a href="Properties.html#TP40014097-CH14-XID_323" data-id="TP40014097-CH14-XID_323" class="">Properties</a>
</li>
<li class="nav-chapter">
<a href="Methods.html#TP40014097-CH15-XID_300" data-id="TP40014097-CH15-XID_300" class="">Methods</a>
</li>
<li class="nav-chapter">
<a href="Subscripts.html#TP40014097-CH16-XID_393" data-id="TP40014097-CH16-XID_393" class="">Subscripts</a>
</li>
<li class="nav-chapter nav-current-chapter">
<a href="Inheritance.html" data-id="TP40014097-CH17-XID_251" class="nav-chapter-active">Inheritance</a>
</li>
<li class="nav-chapter">
<a href="Initialization.html#TP40014097-CH18-XID_266" data-id="TP40014097-CH18-XID_266">Initialization</a>
</li>
<li class="nav-chapter">
<a href="Deinitialization.html#TP40014097-CH19-XID_182" data-id="TP40014097-CH19-XID_182">Deinitialization</a>
</li>
<li class="nav-chapter">
<a href="Automatic Reference Counting.html#TP40014097-CH20-XID_50" data-id="TP40014097-CH20-XID_50">Automatic Reference Counting</a>
</li>
<li class="nav-chapter">
<a href="Optional Chaining.html#TP40014097-CH21-XID_312" data-id="TP40014097-CH21-XID_312">Optional Chaining</a>
</li>
<li class="nav-chapter">
<a href="Type Casting.html#TP40014097-CH22-XID_443" data-id="TP40014097-CH22-XID_443">Type Casting</a>
</li>
<li class="nav-chapter">
<a href="Nested Types.html#TP40014097-CH23-XID_309" data-id="TP40014097-CH23-XID_309">Nested Types</a>
</li>
<li class="nav-chapter">
<a href="Extensions.html#TP40014097-CH24-XID_191" data-id="TP40014097-CH24-XID_191">Extensions</a>
</li>
<li class="nav-chapter">
<a href="Protocols.html#TP40014097-CH25-XID_345" data-id="TP40014097-CH25-XID_345">Protocols</a>
</li>
<li class="nav-chapter">
<a href="Generics.html#TP40014097-CH26-XID_234" data-id="TP40014097-CH26-XID_234">Generics</a>
</li>
<li class="nav-chapter">
<a href="Advanced Operators.html#TP40014097-CH27-XID_28" data-id="TP40014097-CH27-XID_28">Advanced Operators</a>
</li>
</ul>
</li><li data-id="TP40014097-CH28-XID_912" class="part-name">Language Reference
<ul class="nav-chapters">
<li class="nav-chapter">
<a href="About the Language Reference.html#TP40014097-CH29-XID_453" data-id="TP40014097-CH29-XID_453" class="">About the Language Reference</a>
</li>
<li class="nav-chapter">
<a href="Lexical Structure.html#TP40014097-CH30-XID_794" data-id="TP40014097-CH30-XID_794">Lexical Structure</a>
</li>
<li class="nav-chapter">
<a href="Types.html#TP40014097-CH31-XID_988" data-id="TP40014097-CH31-XID_988">Types</a>
</li>
<li class="nav-chapter">
<a href="Expressions.html#TP40014097-CH32-XID_655" data-id="TP40014097-CH32-XID_655">Expressions</a>
</li>
<li class="nav-chapter">
<a href="Statements.html#TP40014097-CH33-XID_913" data-id="TP40014097-CH33-XID_913">Statements</a>
</li>
<li class="nav-chapter">
<a href="Declarations.html#TP40014097-CH34-XID_475" data-id="TP40014097-CH34-XID_475">Declarations</a>
</li>
<li class="nav-chapter">
<a href="Attributes.html#TP40014097-CH35-XID_460" data-id="TP40014097-CH35-XID_460">Attributes</a>
</li>
<li class="nav-chapter">
<a href="Patterns.html#TP40014097-CH36-XID_878" data-id="TP40014097-CH36-XID_878">Patterns</a>
</li>
<li class="nav-chapter">
<a href="Generic Parameters and Arguments.html#TP40014097-CH37-XID_774" data-id="TP40014097-CH37-XID_774">Generic Parameters and Arguments</a>
</li>
<li class="nav-chapter">
<a href="Summary of the Grammar.html#TP40014097-CH38-XID_1030" data-id="TP40014097-CH38-XID_1030">Summary of the Grammar</a>
</li>
</ul>
</li>
</ul>
</nav>
<article class="chapter">
<a name="TP40014097-CH17"></a><a name="TP40014097-CH17-XID_251"></a>
<div class="pixel-line"></div>
<h2 class="chapter-name chapter-name-short">Inheritance</h2>
<section id="mini_toc" class="hideInXcode" role="navigation">
<div id="mini_toc_button">
<p>On This Page</p>
</div>
<ul class="list-bullet">
<li class="item">
<p class="para">
<a href="#TP40014097-CH17-XID_252">
Defining a Base Class
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH17-XID_254">
Subclassing
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH17-XID_257">
Overriding
</a>
</p>
</li>
<li class="item">
<p class="para">
<a href="#TP40014097-CH17-XID_265">
Preventing Overrides
</a>
</p>
</li>
</ul>
</section>
<section class="section">
<p class="para">
A class can <em>inherit</em> methods, properties, and other characteristics from another class. When one class inherits from another, the inheriting class is known as a <em>subclass</em>, and the class it inherits from is known as its <em>superclass</em>. Inheritance is a fundamental behavior that differentiates classes from other types in Swift.
</p>
<p class="para">
Classes in Swift can call and access methods, properties, and subscripts belonging to their superclass and can provide their own overriding versions of those methods, properties, and subscripts to refine or modify their behavior. Swift helps to ensure your overrides are correct by checking that the override definition has a matching superclass definition.
</p>
<p class="para">
Classes can also add property observers to inherited properties in order to be notified when the value of a property changes. Property observers can be added to any property, regardless of whether it was originally defined as a stored or computed property.
</p>
</section>
<section class="section">
<a name="TP40014097-CH17-XID_252"></a>
<h3 class="section-name" tabindex="0">Defining a Base Class</h3>
<p class="para">
Any class that does not inherit from another class is known as a <em>base class</em>.
</p><div class="note">
<a name="TP40014097-CH17-XID_253"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">Swift classes do not inherit from a universal base class. Classes you define without specifying a superclass automatically become base classes for you to build upon.
</p>
</aside>
</div><p class="para">
The example below defines a base class called <code class="code-voice">Vehicle</code>. This base class declares two properties (<code class="code-voice">numberOfWheels</code> and <code class="code-voice">maxPassengers</code>) that are universal to all vehicles. These properties are used by a method called <code class="code-voice">description</code>, which returns a <code class="code-voice">String</code> description of the vehicle’s characteristics:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">class</span> <span class="vc">Vehicle</span> {</code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">numberOfWheels</span>: <span class="n"></span></code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">maxPassengers</span>: <span class="n"></span></code></li>
<li><code class="code-voice"> <span class="kt">func</span> <span class="vc">description</span>() -> <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="s">"</span>\(<span class="vc">numberOfWheels</span>)<span class="s"> wheels; up to </span>\(<span class="vc">maxPassengers</span>)<span class="s"> passengers"</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">init</span>() {</code></li>
<li><code class="code-voice"> <span class="vc">numberOfWheels</span> = <span class="m">0</span></code></li>
<li><code class="code-voice"> <span class="vc">maxPassengers</span> = <span class="m">1</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
The <code class="code-voice">Vehicle</code> class also defines an <em>initializer</em> to set up its properties. Initializers are described in detail in <span class="x-name"><a href="Initialization.html#TP40014097-CH18-XID_266" data-id="TP40014097-CH18-XID_266">Initialization</a></span>, but a brief introduction is required here in order to illustrate how inherited properties can be modified by subclasses.
</p><p class="para">
You use initializers to create a new instance of a type. Although initializers are not methods, they are written in a very similar syntax to instance methods. An initializer prepares a new instance for use, and ensures that all properties of the instance have valid initial values.
</p><p class="para">
In its simplest form, an initializer is like an instance method with no parameters, written using the <code class="code-voice">init</code> keyword:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">init</span>() {</code></li>
<li><code class="code-voice"> <span class="c">// perform some initialization here</span></code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
To create a new instance of <code class="code-voice">Vehicle</code>, call this initializer with <em>initializer syntax</em>, written as <code class="code-voice">TypeName</code> followed by empty parentheses:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">someVehicle</span> = <span class="vc">Vehicle</span>()</code></li>
</ul>
</div>
</section><p class="para">
The initializer for <code class="code-voice">Vehicle</code> sets some initial property values (<code class="code-voice">numberOfWheels = 0</code> and <code class="code-voice">maxPassengers = 1</code>) for an arbitrary vehicle.
</p><p class="para">
The <code class="code-voice">Vehicle</code> class defines common characteristics for an arbitrary vehicle, but is not much use in itself. To make it more useful, you need to refine it to describe more specific kinds of vehicle.
</p>
</section>
<section class="section">
<a name="TP40014097-CH17-XID_254"></a>
<h3 class="section-name" tabindex="0">Subclassing</h3>
<p class="para">
<em>Subclassing</em> is the act of basing a new class on an existing class. The subclass inherits characteristics from the existing class, which you can refine. You can also add new characteristics to the subclass.
</p><p class="para">
To indicate that a class has a superclass, write the superclass name after the original class name, separated by a colon:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">class</span> <span class="vc">SomeClass</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="c">// class definition goes here</span></code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
The next example defines a second, more specific vehicle called <code class="code-voice">Bicycle</code>. This new class is based on the existing capabilities of <code class="code-voice">Vehicle</code>. You indicate this by placing the name of the class the subclass builds upon (<code class="code-voice">Vehicle</code>) after its own name (<code class="code-voice">Bicycle</code>), separated by a colon.
</p><p class="para">
This can be read as:
</p><p class="para">
“Define a new class called <code class="code-voice">Bicycle</code>, which inherits the characteristics of <code class="code-voice">Vehicle</code>”:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">class</span> <span class="vc">Bicycle</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">init</span>() {</code></li>
<li><code class="code-voice"> <span class="kt">super</span>.<span class="kt">init</span>()</code></li>
<li><code class="code-voice"> <span class="vc">numberOfWheels</span> = <span class="m">2</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
<code class="code-voice">Bicycle</code> is a subclass of <code class="code-voice">Vehicle</code>, and <code class="code-voice">Vehicle</code> is the superclass of <code class="code-voice">Bicycle</code>. The new <code class="code-voice">Bicycle</code> class automatically gains all characteristics of <code class="code-voice">Vehicle</code>, such as its <code class="code-voice">maxPassengers</code> and <code class="code-voice">numberOfWheels</code> properties. You can tailor those characteristics and add new ones to better match the requirements of the <code class="code-voice">Bicycle</code> class.
</p><p class="para">
The <code class="code-voice">Bicycle</code> class also defines an initializer to set up its tailored characteristics. The initializer for <code class="code-voice">Bicycle</code> calls <code class="code-voice">super.init()</code>, the initializer for the <code class="code-voice">Bicycle</code> class’s superclass, <code class="code-voice">Vehicle</code>, and ensures that all of the inherited properties are initialized by <code class="code-voice">Vehicle</code> before <code class="code-voice">Bicycle</code> tries to modify them.
</p><div class="note">
<a name="TP40014097-CH17-XID_255"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">Unlike Objective-C, initializers are not inherited by default in Swift. For more information, see <span class="x-name"><a href="Initialization.html#TP40014097-CH18-XID_290" data-id="TP40014097-CH18-XID_290">Initializer Inheritance and Overriding</a></span>.
</p>
</aside>
</div><p class="para">
The default value of <code class="code-voice">maxPassengers</code> provided by <code class="code-voice">Vehicle</code> is already correct for a bicycle, and so it is not changed within the initializer for <code class="code-voice">Bicycle</code>. The original value of <code class="code-voice">numberOfWheels</code> is not correct, however, and is replaced with a new value of <code class="code-voice">2</code>.
</p><p class="para">
As well as inheriting the properties of <code class="code-voice">Vehicle</code>, <code class="code-voice">Bicycle</code> also inherits its methods. If you create an instance of <code class="code-voice">Bicycle</code>, you can call its inherited <code class="code-voice">description</code> method to see how its properties have been updated:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">bicycle</span> = <span class="vc">Bicycle</span>()</code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="s">"Bicycle: </span>\(<span class="vc">bicycle</span>.<span class="vc">description</span>())<span class="s">"</span>)</code></li>
<li><code class="code-voice"><span class="c">// Bicycle: 2 wheels; up to 1 passengers</span></code></li>
</ul>
</div>
</section><p class="para">
Subclasses can themselves be subclassed:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">class</span> <span class="vc">Tandem</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">init</span>() {</code></li>
<li><code class="code-voice"> <span class="kt">super</span>.<span class="kt">init</span>()</code></li>
<li><code class="code-voice"> <span class="vc">maxPassengers</span> = <span class="m">2</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
This example creates a subclass of <code class="code-voice">Bicycle</code> for a two-seater bicycle known as a “tandem”. <code class="code-voice">Tandem</code> inherits the two properties from <code class="code-voice">Bicycle</code>, which in turn inherits these properties from <code class="code-voice">Vehicle</code>. <code class="code-voice">Tandem</code> doesn’t change the number of wheels—it’s still a bicycle, after all—but it does update <code class="code-voice">maxPassengers</code> to have the correct value for a tandem.
</p><div class="note">
<a name="TP40014097-CH17-XID_256"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">Subclasses are only allowed to modify <em>variable</em> properties of superclasses during initialization. You can’t modify inherited constant properties of subclasses.
</p>
</aside>
</div><p class="para">
Creating an instance of <code class="code-voice">Tandem</code> and printing its description shows how its properties have been updated:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">tandem</span> = <span class="vc">Tandem</span>()</code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="s">"Tandem: </span>\(<span class="vc">tandem</span>.<span class="vc">description</span>())<span class="s">"</span>)</code></li>
<li><code class="code-voice"><span class="c">// Tandem: 2 wheels; up to 2 passengers</span></code></li>
</ul>
</div>
</section><p class="para">
Note that the <code class="code-voice">description</code> method is also inherited by <code class="code-voice">Tandem</code>. Instance methods of a class are inherited by any and all subclasses of that class.
</p>
</section>
<section class="section">
<a name="TP40014097-CH17-XID_257"></a>
<h3 class="section-name" tabindex="0">Overriding</h3>
<p class="para">
A subclass can provide its own custom implementation of an instance method, class method, instance property, or subscript that it would otherwise inherit from a superclass. This is known as <em>overriding</em>.
</p><p class="para">
To override a characteristic that would otherwise be inherited, you prefix your overriding definition with the <code class="code-voice">override</code> keyword. Doing so clarifies that you intend to provide an override and have not provided a matching definition by mistake. Overriding by accident can cause unexpected behavior, and any overrides without the <code class="code-voice">override</code> keyword are diagnosed as an error when your code is compiled.
</p><p class="para">
The <code class="code-voice">override</code> keyword also prompts the Swift compiler to check that your overriding class’s superclass (or one of its parents) has a declaration that matches the one you provided for the override. This check ensures that your overriding definition is correct.
</p>
<section class="section">
<a name="TP40014097-CH17-XID_258"></a>
<h3 class="section-name" tabindex="0">Accessing Superclass Methods, Properties, and Subscripts</h3>
<p class="para">
When you provide a method, property, or subscript override for a subclass, it is sometimes useful to use the existing superclass implementation as part of your override. For example, you can refine the behavior of that existing implementation or store a modified value in an existing inherited variable.
</p><p class="para">
Where this is appropriate, you access the superclass version of a method, property, or subscript by using the <code class="code-voice">super</code> prefix:
</p><ul class="list-bullet">
<li class="item"><p class="para">
An overridden method named <code class="code-voice">someMethod</code> can call the superclass version of <code class="code-voice">someMethod</code> by calling <code class="code-voice">super.someMethod()</code> within the overriding method implementation.
</p>
</li><li class="item"><p class="para">
An overridden property called <code class="code-voice">someProperty</code> can access the superclass version of <code class="code-voice">someProperty</code> as <code class="code-voice">super.someProperty</code> within the overriding getter or setter implementation.
</p>
</li><li class="item"><p class="para">
An overridden subscript for <code class="code-voice">someIndex</code> can access the superclass version of the same subscript as <code class="code-voice">super[someIndex]</code> from within the overriding subscript implementation.
</p>
</li>
</ul>
</section>
<section class="section">
<a name="TP40014097-CH17-XID_259"></a>
<h3 class="section-name" tabindex="0">Overriding Methods</h3>
<p class="para">
You can override an inherited instance or class method to provide a tailored or alternative implementation of the method within your subclass.
</p><p class="para">
The following example defines a new subclass of <code class="code-voice">Vehicle</code> called <code class="code-voice">Car</code>, which overrides the <code class="code-voice">description</code> method it inherits from <code class="code-voice">Vehicle</code>:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">class</span> <span class="vc">Car</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">speed</span>: <span class="n"></span> = <span class="m">0.0</span></code></li>
<li><code class="code-voice"> <span class="kt">init</span>() {</code></li>
<li><code class="code-voice"> <span class="kt">super</span>.<span class="kt">init</span>()</code></li>
<li><code class="code-voice"> <span class="vc">maxPassengers</span> = <span class="m">5</span></code></li>
<li><code class="code-voice"> <span class="vc">numberOfWheels</span> = <span class="m">4</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">override</span> <span class="kt">func</span> <span class="vc">description</span>() -> <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="kt">super</span>.<span class="vc">description</span>() + <span class="s">"; "</span></code></li>
<li><code class="code-voice"> + <span class="s">"traveling at </span>\(<span class="vc">speed</span>)<span class="s"> mph"</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
<code class="code-voice">Car</code> declares a new stored <code class="code-voice">Double</code> property called <code class="code-voice">speed</code>. This property defaults to <code class="code-voice">0.0</code>, meaning “zero miles per hour”. <code class="code-voice">Car</code> also has a custom initializer, which sets the maximum number of passengers to <code class="code-voice">5</code>, and the default number of wheels to <code class="code-voice">4</code>.
</p><p class="para">
<code class="code-voice">Car</code> overrides its inherited <code class="code-voice">description</code> method by providing a method with the same declaration as the <code class="code-voice">description</code> method from <code class="code-voice">Vehicle</code>. The overriding method definition is prefixed with the <code class="code-voice">override</code> keyword.
</p><p class="para">
Rather than providing a completely custom implementation of <code class="code-voice">description</code>, the overriding method actually starts by calling <code class="code-voice">super.description</code> to retrieve the description provided by <code class="code-voice">Vehicle</code>. It then appends some additional information about the car’s current speed.
</p><p class="para">
If you create a new instance of <code class="code-voice">Car</code>, and print the output of its <code class="code-voice">description</code> method, you can see that the description has indeed changed:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">car</span> = <span class="vc">Car</span>()</code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="s">"Car: </span>\(<span class="vc">car</span>.<span class="vc">description</span>())<span class="s">"</span>)</code></li>
<li><code class="code-voice"><span class="c">// Car: 4 wheels; up to 5 passengers; traveling at 0.0 mph</span></code></li>
</ul>
</div>
</section>
</section>
<section class="section">
<a name="TP40014097-CH17-XID_260"></a>
<h3 class="section-name" tabindex="0">Overriding Properties</h3>
<p class="para">
You can override an inherited instance or class property to provide your own custom getter and setter for that property, or to add property observers to enable the overriding property to observe when the underlying property value changes.
</p>
<section class="section">
<a name="TP40014097-CH17-XID_261"></a>
<h3 class="section-name" tabindex="0">Overriding Property Getters and Setters</h3>
<p class="para">
You can provide a custom getter (and setter, if appropriate) to override <em>any</em> inherited property, regardless of whether the inherited property is implemented as a stored or computed property at its source. The stored or computed nature of an inherited property is not known by a subclass—it only knows that the inherited property has a certain name and type. You must always state both the name and the type of the property you are overriding, to enable the compiler to check that your override matches a superclass property with the same name and type.
</p><p class="para">
You can present an inherited read-only property as a read-write property by providing both a getter and a setter in your subclass property override. You cannot, however, present an inherited read-write property as a read-only property.
</p><div class="note">
<a name="TP40014097-CH17-XID_262"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">If you provide a setter as part of a property override, you must also provide a getter for that override. If you don’t want to modify the inherited property’s value within the overriding getter, you can simply pass through the inherited value by returning <code class="code-voice">super.someProperty</code> from the getter, as in the <code class="code-voice">SpeedLimitedCar</code> example below.
</p>
</aside>
</div><p class="para">
The following example defines a new class called <code class="code-voice">SpeedLimitedCar</code>, which is a subclass of <code class="code-voice">Car</code>. The <code class="code-voice">SpeedLimitedCar</code> class represents a car that has been fitted with a speed-limiting device, which prevents the car from traveling faster than 40mph. You implement this limitation by overriding the inherited <code class="code-voice">speed</code> property:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">class</span> <span class="vc">SpeedLimitedCar</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">override</span> <span class="kt">var</span> <span class="vc">speed</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">get</span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="kt">super</span>.<span class="vc">speed</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">set</span> {</code></li>
<li><code class="code-voice"> <span class="kt">super</span>.<span class="vc">speed</span> = <span class="vc">min</span>(<span class="vc">newValue</span>, <span class="m">40.0</span>)</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
Whenever you set the <code class="code-voice">speed</code> property of a <code class="code-voice">SpeedLimitedCar</code> instance, the property’s setter implementation checks the new value and limits it to 40mph. It does this by setting the underlying <code class="code-voice">speed</code> property of its superclass to be the smaller of <code class="code-voice">newValue</code> and <code class="code-voice">40.0</code>. The smaller of these two values is determined by passing them to the <code class="code-voice">min</code> function, which is a global function provided by the Swift standard library. The <code class="code-voice">min</code> function takes two or more values and returns the smallest one of those values.
</p><p class="para">
If you try to set the <code class="code-voice">speed</code> property of a <code class="code-voice">SpeedLimitedCar</code> instance to more than 40mph, and then print the output of its <code class="code-voice">description</code> method, you see that the speed has been limited:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">limitedCar</span> = <span class="vc">SpeedLimitedCar</span>()</code></li>
<li><code class="code-voice"><span class="vc">limitedCar</span>.<span class="vc">speed</span> = <span class="m">60.0</span></code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="s">"SpeedLimitedCar: </span>\(<span class="vc">limitedCar</span>.<span class="vc">description</span>())<span class="s">"</span>)</code></li>
<li><code class="code-voice"><span class="c">// SpeedLimitedCar: 4 wheels; up to 5 passengers; traveling at 40.0 mph</span></code></li>
</ul>
</div>
</section>
</section>
<section class="section">
<a name="TP40014097-CH17-XID_263"></a>
<h3 class="section-name" tabindex="0">Overriding Property Observers</h3>
<p class="para">
You can use property overriding to add property observers to an inherited property. This enables you to be notified when the value of the inherited property changes, regardless of how that property was originally implemented. For more information on property observers, see <span class="x-name"><a href="Inheritance.html#TP40014097-CH14-XID_333" data-id="TP40014097-CH14-XID_333">Property Observers</a></span>.
</p><div class="note">
<a name="TP40014097-CH17-XID_264"></a>
<aside class="aside">
<p class="aside-title">Note
</p>
<p class="para">You cannot add property observers to inherited constant stored properties or inherited read-only computed properties. The value of these properties cannot be set, and so it is not appropriate to provide a <code class="code-voice">willSet</code> or <code class="code-voice">didSet</code> implementation as part of an override.
</p>
<p class="para">
Note also that you cannot provide both an overriding setter and an overriding property observer. If you want to observe changes to a property’s value, and you are already providing a custom setter for that property, you can simply observe any value changes from within the custom setter.
</p>
</aside>
</div><p class="para">
The following example defines a new class called <code class="code-voice">AutomaticCar</code>, which is a subclass of <code class="code-voice">Car</code>. The <code class="code-voice">AutomaticCar</code> class represents a car with an automatic gearbox, which automatically selects an appropriate gear to use based on the current speed. <code class="code-voice">AutomaticCar</code> also provides a custom <code class="code-voice">description</code> method to print the current gear.
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">class</span> <span class="vc">AutomaticCar</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">var</span> <span class="vc">gear</span> = <span class="m">1</span></code></li>
<li><code class="code-voice"> <span class="kt">override</span> <span class="kt">var</span> <span class="vc">speed</span>: <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">didSet</span> {</code></li>
<li><code class="code-voice"> <span class="vc">gear</span> = <span class="vc">Int</span>(<span class="vc">speed</span> / <span class="m">10.0</span>) + <span class="m">1</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice"> <span class="kt">override</span> <span class="kt">func</span> <span class="vc">description</span>() -> <span class="n"></span> {</code></li>
<li><code class="code-voice"> <span class="kt">return</span> <span class="kt">super</span>.<span class="vc">description</span>() + <span class="s">" in gear </span>\(<span class="vc">gear</span>)<span class="s">"</span></code></li>
<li><code class="code-voice"> }</code></li>
<li><code class="code-voice">}</code></li>
</ul>
</div>
</section><p class="para">
Whenever you set the <code class="code-voice">speed</code> property of an <code class="code-voice">AutomaticCar</code> instance, the property’s <code class="code-voice">didSet</code> observer automatically sets the <code class="code-voice">gear</code> property to an appropriate choice of gear for the new speed. Specifically, the property observer chooses a gear which is the new <code class="code-voice">speed</code> value divided by <code class="code-voice">10</code>, rounded down to the nearest integer, plus <code class="code-voice">1</code>. A speed of <code class="code-voice">10.0</code> produces a gear of <code class="code-voice">1</code>, and a speed of <code class="code-voice">35.0</code> produces a gear of <code class="code-voice">4</code>:
</p><section class="code-listing">
<span class="caption"></span>
<div class="code-sample">
<ul class="code-lines">
<li><code class="code-voice"><span class="kt">let</span> <span class="vc">automatic</span> = <span class="vc">AutomaticCar</span>()</code></li>
<li><code class="code-voice"><span class="vc">automatic</span>.<span class="vc">speed</span> = <span class="m">35.0</span></code></li>
<li><code class="code-voice"><span class="vc">println</span>(<span class="s">"AutomaticCar: </span>\(<span class="vc">automatic</span>.<span class="vc">description</span>())<span class="s">"</span>)</code></li>
<li><code class="code-voice"><span class="c">// AutomaticCar: 4 wheels; up to 5 passengers; traveling at 35.0 mph in gear 4</span></code></li>
</ul>
</div>
</section>
</section>
</section>
</section>
<section class="section">
<a name="TP40014097-CH17-XID_265"></a>
<h3 class="section-name" tabindex="0">Preventing Overrides</h3>
<p class="para">
You can prevent a method, property, or subscript from being overridden by marking it as <em>final</em>. Do this by writing the <code class="code-voice">@final</code> attribute before its introducer keyword (such as <code class="code-voice">@final var</code>, <code class="code-voice">@final func</code>, <code class="code-voice">@final class func</code>, and <code class="code-voice">@final subscript</code>).
</p><p class="para">
Any attempts to override a final method, property, or subscript in a subclass are reported as a compile-time error. Methods, properties or subscripts that you add to a class in an extension can also be marked as final within the extension’s definition.
</p><p class="para">
You can mark an entire class as final by writing the <code class="code-voice">@final</code> attribute before the <code class="code-voice">class</code> keyword in its class definition (<code class="code-voice">@final class</code>). Any attempts to subclass a final class will be reported as a compile-time error.
</p>
</section>
<section id="next_previous" class="">
<p class="previous-link"><a href="Subscripts.html#TP40014097-CH16-XID_393" data-id="TP40014097-CH16-XID_393">Subscripts</a></p>
<p class="next-link"><a href="Initialization.html#TP40014097-CH18-XID_266" data-id="TP40014097-CH18-XID_266">Initialization</a></p>
<p class="copyright">Copyright © 2014 Apple Inc. All rights reserved. <a class="link" href="http://www.apple.com/legal/terms/site.html" target="_blank" rel="external">Terms of Use</a> | <a class="link" href="http://www.apple.com/privacy/" target="_blank" rel="external">Privacy Policy</a> | Updated: 2014-06-02
</p>
</section>
</article>
</div>
<div id="big_button"></div>
<footer id="footer">
<div class="content-wrapper">
</div>
<script src="./resource/devpubs-1.1.15.js" type="text/javascript" charset="utf-8"></script> </footer> <div id="blackout"> <div id="preload"></div> </div> </body></html>