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

Arthur Boilanger
Arthur Boilanger
3,372 Points

You need to override the implementation for the getFullName() method to pass the challenge...

I'm not sure where exactly I'm going wrong with this one. Code seems to be compiling correctly in xCode, and I'm seeing a "Dr. Smith" result in the end, so I'm not sure what's going on.

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: String, lastName: String) {
        super.init(firstName: firstName, lastName: lastName)
    }
    override func getFullName() -> String {
        return "\(firstName) \(lastName)"
    }
}
let someDoctor = Doctor(firstName: "Dr.", lastName: "Smith")
someDoctor.getFullName()

2 Answers

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Arthur Boilanger,

You don't need to override the initializer because nothing is being changed. You also don't need to call the initializer, it is done automatically. We are overriding the getFullName function so that it returns "Dr. (lastName)". We don't want to have to pass in "Dr." as a firstName each time because we know it will be constant. That's why we're overriding the original method for this new subclass of Person.

class Doctor: Person {

/* here you need to override the getFullName function
to return "Dr. \(last name)" */

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

}

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

Good Luck!

Arthur Boilanger
Arthur Boilanger
3,372 Points

Wow. That makes so much more sense, and requires so little work overall!

Thanks for the help, really helped me to understand things a bit better I think!

Steven Deutsch
Steven Deutsch
21,046 Points

If there's something I can clarify further just let me know.