TL ; DR:
Put it all together: an abstract parent class, three concrete subclasses, and a polymorphic loop that dispatches by runtime type. This is the shape of nearly every real OOP design.
class Animal {
var name: String
init(_ name: String) { self.name = name }
func sound() -> String { return "" } // each subclass picks
func speak() { // shared by all
print("\(name): \(sound())")
}
}
// Dog, Cat, Bird each override sound()
// let zoo: [Animal] = [Dog("Rex"), ...]
// for a in zoo { a.speak() }
Full lesson:
This problem stitches together everything you've learned in this chapter:
- Inheritance — three concrete classes share a common parent.
- Constructors — each subclass forwards a name to the parent.
- Abstract methods — the parent declares
sound()with no body; each child must provide one. - Shared behavior — the parent has a concrete
speak()that callssound(); every subclass inherits it for free. - Polymorphism — a collection typed as
Animalholds a Dog, a Cat, and a Bird;speak()dispatches to the rightsound()at runtime.
The shape of this code is the shape of nearly every real OOP design you'll see — a small contract at the top (the abstract parent), concrete variants below, and the rest of the program written against the abstract type. New variants get added without touching the consumer code.
Your task
The abstract Animal class is already defined, with a concrete speak() that calls the abstract sound(). Add three subclasses — Dog (returns "Woof"), Cat (returns "Meow"), and Bird (returns "Tweet"). Then in MainFunction / main, build a polymorphic collection holding one of each (Rex, Whiskers, Tweety in that order) and loop through it calling speak().
Expected output:
Rex: Woof
Whiskers: Meow
Tweety: TweetFree plan: 30 tutor messages. Premium is unlimited.