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

I dont understand how to complete this challenge??

I dont understand how to do this challenge I know that I know how to do it but I just can't figure out what I am missing. PLEASE HELP?? I need this to move on since this is a. Ending challenge.

DIRECTIONS I've provided a base class Person in the editor below. Once an instance of Person is created, you can call getFullName() and get a person's full name.

Your job is to create a class named Doctor that overrides the getFullName() method. Once you have a class definition, create an instance and assign it to a constant named someDoctor.

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".

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

Sorry for the mix match format I dont know why it does that but please try to read and help me. Much appreciated.

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

That is what has already been written for me

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello:

You are doing well.. You've implemented the class and the override of the function looks good. The problem you are having is that you are not creating the instance the proper way.

To create an instance of "Doctor" you have to first call the init method, and initialize the properties by passing it the string values for the firstName and lastName.

Remember since you are inheriting from "Person" then you use the init that was already implemented in that class. If you wanted "Doctor" to have additional properties, then you would have to implement a new init method in the "Doctor" class.

// Creating an instance...
let someDoctor = Doctor(firstName: "Jhoan", lastName: "Arango")

After you created then instance, then you can call the function

someDoctor.getFullName()

// This will return Dr. Arango

Also, just to point out on another error, you are passing an argument to a function that has no parameters. The function signature does not have parameters, therefore you should not pass it any arguments.

Good luck

YYYYEEESSSS!!!!!!!!! YYYYYESSSS!!!!!!!!! YYYYYEEESSSS!!!!!!!!!!! THANK YOU SO MUCH!!!!!!!!!!

I have been in a little bit of a jam and needed this done quick THANK YOU SO VERY MUCH!!!!!!