Updated for Xcode 13.3
Now that you understand how classes work, and, just as importantly, how they are different from structs, it’s time to tackle a small challenge to check your progress.
Your challenge is this: make a class hierarchy for animals, starting with
Animal at the top, then
Cat as subclasses, then
Poodle as subclasses of
Lion as subclasses of
But there’s more:
Animalclass should have a
legsinteger property that tracks how many legs the animal has.
Dogclass should have a
speak()method that prints a generic dog barking string, but each of the subclasses should print something slightly different.
Catclass should have a matching
speak()method, again with each subclass printing something different.
Catclass should have an
isTameBoolean property, provided using an initializer.
I’ll provide some hints in a moment, but first I recommend you go ahead and try it yourself.
Hacking with Swift+ subscribers can get a complete video solution for this checkpoint here: Solution to Checkpoint 7. If you don’t already subscribe, you can start a free trial today.
Still here? Okay, here are some hints:
class SomeClass: OtherClass.
speak()method to their parent by using the
Animalclass inside the
SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.
Link copied to your pasteboard.