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 Class Inheritance Overriding Methods

Tyler Dotson
PLUS
Tyler Dotson
Courses Plus Student 1,740 Points

what am i doing wrong?

someone please help me finish this.

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

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

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

// Enter your code below

class Doctor: Person {
    override init(firstName: String, lastName: String) {
        super.init(firstName: String, lastName: String)
        var firstName = firstName
        var lastName = lastName
    }
}

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

3 Answers

In the Doctor class, you need to override the function fullName(), in order to change it's behavior to conform to the challenge requirement. Since fullName() is a function that dictates the behavior performed when Doctor is called, you must use override func, not init.

class Person {
    let firstName: String
    let lastName: String

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

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

// Enter your code below
class Doctor: Person { 
    override func fullName() -> String { 
        return "Dr. \(lastName)"
    }
}

let someDoctor = Doctor(firstName: "FirstName", lastName: "LastName") 
james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

it wants you to override fullName to pass. you don't need to override the init, the Doctor class inherits from Person so it inherits the init. in Doctor, override the fullName from Person and change the body to return what the challenge asks for, then create a Doctor instance with the name Sam Smith. the first name will not be Dr. Dr. is what your overridden fullName function will return along with the last name.

Raed Alahmari
Raed Alahmari
12,966 Points

Here you go ^_^

class Doctor: Person {
    override init(firstName: String, lastName: String) {
        let firstName = firstName
        let lastName = lastName
        super.init(firstName: firstName, lastName: lastName)
    }
}
Martin Wildfeuer
Martin Wildfeuer
Courses Plus Student 11,071 Points

Hey Raed Alahmari! Thanks for contributing to the community, I have changed this from comment to an answer. However, please note that solution only answers are strongly discouraged. Please update your answer with why your solution works compared to the OP.

Thanks a lot!