Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Object-Oriented Swift 2.0 Class Inheritance Overriding Methods

completely lost here, please help

evening guys,

Completely lost here!

am I still supposed to be overriding the init as well as the function?

So many possibilities running through my head for this, any help with explanations would be very very much appreciated.

Thankyou :)

classes.swift
class Person {
    let firstName: String
    let lastName: String

    init(firstName: String, lastName: String) {
        self.firstName = firstName
        self.lastName = lastName
    }

    func getFullName() -> String {
        return "\(firstName) \(lastName)"
    }
}

// Enter your code below

class Doctor: Person {

    override init(firstName: "Dr", lastName: String){
    super.init(firstName: firstName, lastName: lastName)
    }

    override func getFullName() -> String {
        return "\(firstName). \(lastName)"
    }
}

let someDoctor = Doctor.getFullName(firstName: "Sam", lastName: "Smith")

3 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Matt,

If we think about this logically, all we need to do is add "Dr." to and remove the first name from the person's "full" name, but we definitely don't want to change the person's first name just because they got a medical degree :).

Instead, simply override the getFullName() method so it returns "Dr." in place of the first name.

solution.swift
class Person {
    let firstName: String
    let lastName: String

    init(firstName: String, lastName: String) {
        self.firstName = firstName
        self.lastName = lastName
    }

    func getFullName() -> String {
        return "\(firstName) \(lastName)"
    }
}

// Enter your code below
class Doctor: Person {

    override func getFullName() -> String {  // this is the only method to override
        return "Dr. \(lastName)"
    }
}

let somePerson = Person(firstName: "Sam", lastName: "Smith")
somePerson.getFullName()

let someDoctor = Doctor(firstName: "Sam", lastName: "Smith")
someDoctor.getFullName() // Returns "Dr. Smith"

Let me know if this makes sense!

It does, thank you.

We didn't have to override the initializer because we didn't actually change any of the stored properties?

Greg Kaleka
Greg Kaleka
39,021 Points

Exactly - nothing about initialization needs to be changed. There are times when you might need to override an init method for reasons other than changing stored properties, but in this case that's all we have, so your reasoning is correct.

Thanks, Greg!