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 trialJosh Fernandez
1,096 PointsCode 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
Courses Plus Student 11,071 PointsHey 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 :)
Tyron Spencer
1,489 PointsMartin Wildfeuer Thankyou so much, this has also helped too. Cheers
Lana Wong
3,968 PointsWhy do you not put an init method?
Josh Fernandez
1,096 PointsJosh Fernandez
1,096 PointsWow. 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.
sugabelly
2,524 Pointssugabelly
2,524 PointsHi, 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