Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,018 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,018 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!