O'Reilly logo

Mastering Swift 2 by Jon Hoffman

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

Overriding methods and properties

To override a method, property, or subscript, we need to prefix the definition with the override keyword. This tells the compiler that we intend to override something in the super class and that we did not make a duplicate definition by mistake. The override keyword does prompt the Swift compiler to verify that the super class (or one of its parents) has a matching declaration that can be overridden. If it cannot find a matching declaration in one of the super classes, an error will be thrown.

Overriding methods

Let's look at how we would override a method. We will start by adding a getDetails() method to the Plant class that we will then override in the child classes. The following code shows how the new Plant ...

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