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 and in the Swift Sandbox. The code file for the sample is included in the swift_3_oop_chapter_04_05 folder:

Overriding properties

Tip

When we declare either a type property or a method with the static keyword in a base class, it isn't possible to override it in a subclass. Thus, if we want to enable either a type property or a method to be overridden in the subclasses, it is necessary to use the class keyword instead of static when we declare them in the base class.

We have to change the declaration of the type properties declared in the Animal class to use the class keyword instead of the static keyword. The following lines show the first lines of code of the new version of the Animal class that replaces the declaration of the type properties to make it possible to override them in its subclasses. Note that the rest of the code for the class after the declaration of the three type properties (numberOfLegs, averageNumberOfChildren, and abilityToFly) remains without changes. The code file for the sample is included in the swift_3_oop_chapter_04_06 folder:

    open class Animal { 
      open class var numberOfLegs: Int { 
        get { 
          return 0;            
        } 
      } 
 
      open class var averageNumberOfChildren: Int { 
        get { 
          return 0; 
        } 
      } 
     
      open class var abilityToFly: Bool { 
        get { 
          return false; 
        } 
      } 
   ... 
    } 

After we make the preceding changes to the Animal class, we will notice that the Playground will remove the error message in the declaration of the type property we declared in the Dog class. In fact, we didn't have to make changes to the type property declaration in the Dog class to remove the error. However, we must take into account that the usage of the static keyword when declaring the numberOfLegs type property in the Dog class that overrides the inherited property from the Animal class prevents subclasses of Dog from overriding this property. When we use static for overridden type properties, we are indicating to Swift that we don't want the type property to be overridden any more. In this case, it makes sense because so far, all the dogs that have been discovered have four legs. Thus, any Dog subclass won't need to specify a different value for this type property.

The following line prints the value for the overridden type property. The code file for the sample is included in the swift_3_oop_chapter_04_06 folder:

    print(Dog.numberOfLegs) 

The next screenshot shows the results of printing the overridden type property in the Playground after we edited the type properties declarations in the Animal class:

Overriding properties

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.137.10