Chapter    39

Inheritance

Classes have the ability to inherit methods and properties from a parent class. Inheritance encourages code reuse. Generally, when you inherit a class, you will add custom properties and methods to the new class.

For instance, if you wanted to create a new class to manage the employees in your small business, you may want to inherit your work from the Parent class into a new class called Employee. Listing 39-1 shows how you would do that.

Listing 39-1. Inheriting Person

class Person {    var name: String = "Name"    var age:Int = 0    func profile() -> String {        return "I'm \(self.name) and I'm \(self.age) years old."    }}class Employee: Person {}

In Listing 39-1, your new class Employee is inheriting Person ...

Get Swift Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.