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

Ryan Maneo
Ryan Maneo
4,342 Points

Unable to understand instruction

So frustrating. I simply do not understand the instruction AT ALL. A few questions:

  1. Can someone please rephrase this question with more detail and instruction?

  2. Can someone give me tips on how to read programming instructions? Confused?

Thanks... :/

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 {

override init getFullName {



}

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

3 Answers

Make sure that you changed

 class Doctor {

to

 class Doctor:Person {

Hey Ryan,

You're very close! Let's go through the errors one by one as we update the code:

 error: method does not override any method from its superclass

This means that your override function getFullName is not overriding anything, because it doesn't see what it's supposed to override. This is because the class Doctor does not inherit anything from class Person as it's currently not a subclass of Person. So to solve this error, you'll want to change your Doctor class to:

 class Doctor:Person {

Next let's look at this error:

 error: expected '(' for initializer parameters
 override init getFullName {

Look at your override funciton getFullName. Functions must be followed by parenthesis, so change it to:

 override init getFullName() {

There's one other thing to change here before we try to recompile: we don't need to override the init, we want to override the function getFullName. Change init to func.

 override func getFullName() {

Once that's changed and you try to compile, the next error is:

 error: method does not override any method from its superclass
 override func getFullName() {

This might be a bit confusing as it doesn't exactly tell you what's wrong, but what's missing is a typecast:

 override func getFullName()  -> String {

Compiling from there, the next error tells us that we aren't returning anything:

 error: missing return in a function expected to return 'String'

This is where we need to change how the getFullName function behaves (what exactly we are overriding in the subclass function). In the challenge instructions, it asks you to return "Dr." followed by the last name:

    return "Dr. \(lastName)"
Ryan Maneo
Ryan Maneo
4,342 Points

Doesn't seem to work... http://hastebin.com/wecigebiqu.coffee The error said I need to check the implementation of the "Person" inheritance?

Ryan Maneo
Ryan Maneo
4,342 Points

Re-read the code if you can... it is already that... http://hastebin.com/wecigebiqu.coffee

Looks like you have a space after the colon.

But a different error? I see you didn't use the return value from above:

  return "Dr. \(lastName)"