TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How does my Checkpoint 7 look? It feels too simple but maybe I just understood classes?

Forums > 100 Days of SwiftUI

class Animal {
    let legs: Int

    init(legs: Int) {
        self.legs = legs
    }
}

class Dog: Animal {
    func speak() {
        print("Bark Bark")
    }
}

class Cat: Animal {
    var isTame: Bool
    func speak() {
        print("Meow Meow")
    }

    init(legs: Int, isTame: Bool) {
        self.isTame = isTame
        super.init(legs: legs)
    }
}

class Corgi: Dog {
    override func speak() {
        print("Bork Bork")
    }
}

class Poodle: Dog {
    override func speak() {
        print("BAHHH")
    }
}

class Persian: Cat {
    override func speak() {
        print("MEEOWW")
    }
    override init(legs: Int, isTame: Bool) {
        super.init(legs: legs, isTame: isTame)
    }
}

class Lion: Cat {
    override func speak() {
        print("ROAR")
    }
    override init(legs: Int, isTame: Bool) {
        super.init(legs: legs, isTame: isTame)
    }
}

let sammy = Lion(legs: 4, isTame: false)
sammy.speak()

let tuna = Corgi(legs: 4)
tuna.speak()

let dots = Poodle(legs: 4)
dots.speak()

let kitty = Persian(legs: 4, isTame: true)
kitty.speak()

2      

Here are some thoughts. You'll start to recognize these patterns later on in the course.

If you see redundant code, this is a sign to you that perhaps you can move the code to the parent class. Can you spot the redundant code here?

class Persian: Cat {
    override func speak() {
        print("MEEOWW")
    }
    // Compare to code in Lion class
    override init(legs: Int, isTame: Bool) {
        super.init(legs: legs, isTame: isTame)
    }
}

class Lion: Cat {
    override func speak() {
        print("ROAR") // <-- Change the default behaviour. Excellent.
    }
    // Compare to code in Persian class
    override init(legs: Int, isTame: Bool) {
        super.init(legs: legs, isTame: isTame)
    }
}

The same code is in two derived classes. Both are subclassed from Cat. So maybe this redundant code belongs in the Cat class, instead? But take a closer look. Maybe you're not really overriding anything here?

Indeed, you are just calling the super class' initializer and not making any changes. Let's mix it up.

Override a Superclass Method

So why might you want to override the Cat initializer? Well, Cat gives you the option to designate whether a cat is tame or not. Think about the use case: would you ever consider a Lion to be tame?

So consider the case where you instantiate a Lion object in your code as you did in your example above:

let sammy = Lion(legs: 4, isTame: false)  // <-- Would you consider a lion tame by default?
sammy.speak()

// Consider instead
class Lion: Cat {
    override func speak() {
        print("ROAR") // <-- Change the default behavior
    }
    // Lions have specific properties that are different from domestic cats.
    init(legs: Int ) {
        super.init(legs: legs, isTame: false)  // <-- Lions are never tame. Even captive lions!
    }
}

let leoTheLion = Lion(legs: 4) // <-- initializer always sets isTame to false

Keep coding

2      

Ah I see, appreciate the response. This makes sense and I was actually wondering about this. When I would write init and press return for those 2 subclasses, it automatically just put override but I did notice I wasn't actually overriding anything. Thanks for the tip!

2      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.