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

Calvin Liem
Calvin Liem
3,599 Points

help with challenge

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

why do we need to overide the func getFullName()

can't we just make empty sub class of Doctor like this?

i don't know what to type in overide func getFullName() i ended up with writing the same code as parentclass func getFullName()

can somebody help me

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 {



}

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


// Enter your code below

2 Answers

the idea should to change the behaviour of the doctor class with the same parameters. So "inside" the doctor class you use the "override" keyword to say the compiler that you want to override the parent method.

Try this:

class Doctor: Person {

    override func getFullName() -> String {
        return "Dr. \(lastName)"
    }
}

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

This will return "Dr. Smith" because it's a doctor class; if you use a Person class with the same parameters values it will return "Sam Smith"

Greg Kaleka
Greg Kaleka
39,021 Points

Yep - this is not only the correct solution for the challenge, but it's also the "proper" way to do it. Dr. Sam Smith's first name is not Dr. Imagine you had an app and in some cases you wanted to use the user's full name, but in others you wanted to just use the first name. You wouldn't want to use "Dr." in those cases.

Calvin Liem
Calvin Liem
3,599 Points

owh i get the idea now.. thanks