O'Reilly logo

Swift Quick Syntax Reference by Matthew Campbell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required