Overriding properties

First, we will try to override the numberOfLegs type property that the Dog class will inherit from the Animal base class. We will face an issue and solve it. The following lines show the code for a simplified version of the Dog class that inherits from DomesticMammal and just tries to override the numberOfLegs type property:

    open class Dog: DomesticMammal { 
      open static override var numberOfLegs: Int { 
        get { 
            return 4; 
        } 
      } 
    } 

After we enter the previous lines in the Playground, we will see the following error message in the line that tries to override the numberOfLegs type property: error: cannot override static var. The following screenshot shows the error in the Playground. We will see similar error messages in the Swift REPL ...

Get Swift 3 ObjectOriented Programming - Second Edition 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.