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

Michael Williams
PLUS
Michael Williams
Courses Plus Student 8,059 Points

Swift Inheritance: overriding func not working

Following are some errors from the code that follows. First, I understand what's going on with the last error: I'm returning something when it's expecting me to return nothing because it hasn't been defined. To fix it, I need to add a return value for the function I'm overriding. However, the question is why do I still have to have a return value that's been inherited? Shouldn't that be implied?

swift_lint.swift:40:19: error: method does not override any method from its superclass     override func fullName(){ 
^ swift_lint.swift:29:8: note: potential overridden instance method 'fullName()' here   func fullName() -> String {
^ swift_lint.swift:41:16: error: unexpected non-void return value in void function         return "Dr. \(lastName)"                ^~~~~~~~~~~~~~~~~
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: firstName, lastName: lastName)
    }

    override func fullName() String {
        return "Dr. \(lastName)"
    }

}

let someDoctor = Doctor(firstName: "Turd", lastName: "Ferguson")

2 Answers

tromben98
tromben98
13,273 Points

Hi Michael!

If you look through the function you can notice that you forgot to mark the return type with the return arrow :

->

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: firstName, lastName: lastName)
    }
                                         // Look here!!!!!!!!!!
    override func fullName()  -> String {
        return "Dr. \(lastName)"
    }

}

let someDoctor = Doctor(firstName: "Turd", lastName: "Ferguson")
Michael Williams
PLUS
Michael Williams
Courses Plus Student 8,059 Points

Hi there. Thanks for your help. I understand that the return portion is missing, but my question is why does it have to be there if we're inheriting it from the superclass? Because writing out the return type is repeating code.

I am pretty sure this is because you need to print the "Dr."