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

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…

Contents