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

Asai Andrade
Asai Andrade
7,170 Points

This works perfectly in Xcode.. But why won't it work for the challenge?

class Doctor: Person {
    override init(firstName: String, lastName: String) {
        super.init(firstName: "Dr.", lastName: "Smith")
    }

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


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

let someDoctor = Doctor(firstName: "Dr.", lastName: "Smith")
someDoctor.getFullName()
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)"
    }
}


class Doctor: Person
{
    override init(firstName: String, lastName: String)
    {
        super.init(firstName: "Dr.", lastName: "Smith")
    }

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


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

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

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Asai,

You're not quite following the directions. The directions ask you to override the getFullName() method. You have typed override, but you're not making any changes. Also, we don't want to override the init() method, and we don't want to modify the person's full name!

Instead, override the getFullName() method so it returns "Dr." in place of the first name (this way we don't change the first name at all).

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") // Don't use "Dr." here. That's cheating!
someDoctor.getFullName()
Asai Andrade
Asai Andrade
7,170 Points

Hey Greg Kaleka,

Thank you so much for clearing that up! I made it much more complicated then it should have been. Just a quick question...inherited classes don't need to be initialized since the parent class is already initialized right? And we only need to initialize the inherited class if we want to override it?

Thank you once again! :)

Greg Kaleka
Greg Kaleka
39,021 Points

Asai,

You've got the right idea. All methods in the super class are essentially copied over to sub classes, and do exactly the same thing, unless you override them. Since they're copied over, there's no reason to override them unless we're changing something about them.

Initialization is done through the init method and is no exception. We're not not initializing by not overriding the init method, we're just saying that we're cool with using the super class's init method exactly as it stands.

Hope that makes sense!

Asai Andrade
Asai Andrade
7,170 Points

Greg Kaleka,

No, that makes perfect sense! Thanks for clearing that up. Was a tad bit confused but now I fully understand it. Thanks so much for this! I really appreciate it.