Swift Inheritance
A class can inherit from one superclass. override replaces a method and super calls the parent. final stops further subclassing.
Example
class Animal { func speak() -> String { "..." } }
class Dog: Animal { override func speak() -> String { "Woof" } }
class Cat: Animal { override func speak() -> String { "Meow" } }
for a in [Dog(), Cat()] { print(a.speak()) }Exercise
Replace a method.
func speak()Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.