From the course: Swift 6 Essential Training
Unlock this course with a free trial
Join today to access over 24,700 courses taught by industry experts.
Subclassing in Swift - Swift Tutorial
From the course: Swift 6 Essential Training
Subclassing in Swift
- [Instructor] A great way to expand the flexibility of your custom classes, without having to reinvent the wheel, is to create subclasses that share functionality with their parent. For this example, we're going to create a subclass of Adventurer, we're going to call it Ranger and change some of its inherit properties. Under the subclass comment, and declare a new class, name it Ranger, and a subclass we just add a colon and the name of the parent class we want. (keyboard clicking) Depending on the access level of the parent, a subclass is going to have access to its properties, methods and other entities. In this case Ranger is going to inherit name, special move, maxHealth and our computer properties. So there's no need to duplicate that code. However, if we want Ranger to have unique properties that the Adventurer parent class doesn't have, we can declare them. So we'll say var classAdvantage, and this is…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
(Locked)
Populating the game world26s
-
(Locked)
Value vs. reference types2m 43s
-
Basic Swift classes6m 24s
-
(Locked)
Access modifiers and properties7m 29s
-
(Locked)
Subclassing in Swift4m 21s
-
(Locked)
Basic Swift structs6m 9s
-
(Locked)
Chaining optionals6m 33s
-
(Locked)
Challenge: Inventory system34s
-
(Locked)
Solution: Inventory system5m 13s
-
(Locked)
-
-