Combining inheritance and protocols

We can combine class inheritance with protocol conformance. The following lines show the code for a new AngryCatAlien class that inherits from the AngryCat class and conforms to the Alien protocol. Note that the class declaration includes the superclass (AngryCat) and the implemented protocol (Alien) separated by a comma after the colon (:). The code file for the sample is included in the swift_3_oop_chapter_05_09 folder:

    open class AngryCatAlien : AngryCat, Alien { open var numberOfEyes: Int = 0 init (nickName: String, age: UInt, fullName: String, initialScore: UInt, x: UInt, y: UInt, numberOfEyes: Int) { super.init(nickName: nickName, age: age, fullName: fullName, initialScore: initialScore, x: x, y: y) self.numberOfEyes ...

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.