O'Reilly logo

Mastering Swift 4 - Fourth Edition 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 properties

We can provide custom getters and setters to override any inherited property. When we override a property, we must provide the name and the type of property we are overriding, so the compiler can verify one of the classes in the class hierarchy has a matching property to override.

Let's see how we can override a property by adding the following property to our Plant class:

var description: String {  
  return "Base class is Plant." 
} 

The description property is a basic read-only property. This property returns the string, Base class is Plant. Now let's override this property by adding the following property to the Tree class:

override var description: String { 
  return "\(super.description) I am a Tree class." 
} 

The same ...

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