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

Jeff Ripke
Jeff Ripke
41,989 Points

Unable to figure this out, no errors in xCode. Does not pass.

Does not pass, not sure why.

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: firstName, lastName: lastName)
  }
  override func getFullName() -> String {
    return "\(firstName) \(lastName)"
  }
}

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

2 Answers

Hi Jeff,

You're right, Xcode will not return errors. This is because you've written perfectly valid code. However, let's take a closer look at the last sentence of the challenge:

'For example, given the first name "Sam", and last name "Smith", calling getFullName() on an instance of Person would return "Sam Smith", but calling the same method on an instance of Doctor would return "Dr. Smith".'

As you can see from your Xcode results, someDoctor.getFullName() is still returning the value "Sam Smith". It should return "Dr. Smith". So while your code is valid, it doesn't achieve the result the challenge calls for.

They key is in adjusting your override function getFullName in the Doctor subclass to return the correct string.

Hope this helps!

Jeff Ripke
Jeff Ripke
41,989 Points

That worked thanks.