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

Josh Fernandez
Josh Fernandez
1,096 Points

Code Challenge 1 of 1

Challenge Task 1 of 1

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

I'm having a really hard time trying to figure out how to override the getFullName() method in the Doctor class. The last challenge for creating a Car subclass seemed way to easy compared to this one. Any insights would be appreciated!

2 Answers

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

Hey there! Please find below the working code with my explanation in the comments.

// The base class
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)"
    }
}
// Our Doctor class inherits from the Person class,
// therefore also inherits all initializers, methods
// and properties that are defined in Person
class Doctor: Person {

  // If you want to change the implementation of a method
  // that already exists in your superclass, make sure to use
  // the override keyword.
  override func getFullName() -> String {
    return "Dr. \(lastName)"
  }
}

// As mentioned above, Doctor inherits initializers from Person
// and thus can be initialized just like that:
let someDoctor = Doctor(firstName: "Josh", lastName: "Fernandez")

// Now let's call the overridden getFullName method
let fullName = someDoctor.getFullName() // Dr. Fernandez

Hope that helps :)

Josh Fernandez
Josh Fernandez
1,096 Points

Wow. Thank so much. I have a bad habit of overcomplicating my code. Thank you for the all the specificity your notes as well. That demystify a lot of this stuff for me.

Hi, Can you explain this line below, but instead of overriding the func, please explain how to override the init of the base class using "override init".

Thank you

// If you want to change the implementation of a method
  // that already exists in your superclass, make sure to use
  // the override keyword.
  override func getFullName() -> String {
    return "Dr. \(lastName)"
  }
}
Tyron Spencer
Tyron Spencer
1,489 Points

Martin Wildfeuer Thankyou so much, this has also helped too. Cheers

Lana Wong
Lana Wong
3,968 Points

Why do you not put an init method?