Declaring subclasses that inherit the conformance to protocols

We have an Animal class that conforms to both the AnimalProtocol and Equatable protocols. Now, we will create a subclass of Animal, a Dog class, which overrides the string computed properties defined in the Animal class to provide the appropriate values for a dog. The code file for the sample is included in the swift_3_oop_chapter_06_03 folder:

    open class Dog: Animal { 
      open override var spelledSound1: String { 
        get { 
          return "Woof" 
        } 
      } 
     
      open override var spelledSound2: String { 
        get { 
          return "Wooooof" 
        } 
      } 
     
      open override var spelledSound3: String { 
        get { 
          return "Grr" 
        } 
      } 
     
      open override var danceCharacters: String { 
        get { 
          return "/-\ \-\ /-/" 
        } 
      } 
    } 

With just a few additional lines of code, we will create another subclass of Animal, which is a Frog class that also overrides the string's read-only properties defined in the Animal class to provide the appropriate values for a frog, as follows. The code file for the sample is included in the swift_3_oop_chapter_06_03 folder:

    open class Frog: Animal { 
      open override var spelledSound1: String { 
        get { 
          return "Ribbit" 
        } 
      } 
     
      open override var spelledSound2: String { 
        get { 
          return "Croak" 
        } 
      } 
     
      open override var spelledSound3: String { 
        get { 
          return "Croooaaak" 
        } 
      } 
     
      open override var danceCharacters: String { 
        get { 
          return "/|\ \|/ ^ ^ " 
        } 
      } 
    } 

Finally, we will create another subclass of Animal, which is a Lion class that also overrides the string's read-only properties defined in the Animal class to provide the appropriate values for a lion, as follows. The code file for the sample is included in the swift_3_oop_chapter_06_03 folder:

    open class Lion: Animal {
  open override var spelledSound1: String { 
        get { 
          return "Roar" 
        } 
      } 
     
      open override var spelledSound2: String { 
        get { 
          return "Rrroarrr" 
        } 
      } 
           open override var spelledSound3: String { 
        get { 
          return "Rrrrrrroarrrrrr" 
        } 
      } 
     
      open override var danceCharacters: String { 
        get { 
          return "*-* ** *|* ** " 
        } 
      } 
    } 

We have three classes that inherit the conformance to protocols from its base class, which is Animal. The following three classes conform to both the AnimalProtocol and Equatable protocols, without including the conformance within the class declaration, but inheriting it:

  • Dog
  • Frog
  • Lion
..................Content has been hidden....................

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