O'Reilly logo

Swift 3 ObjectOriented Programming - Second Edition by Gastón C. Hillar

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

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

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