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 trialMarina Alenskaja
9,320 PointsWhy didn't I need to use an initializer for the code to work?
Hi
I'm a bit confused - I solved the challenge, but I don't understand why I didn't have to write an init method for the subclass Doctor, for the code to work?
Here is what I wrote:
class Doctor: Person {
override func getFullName() -> String {
return ("Dr. \(lastName)")
}
}
let someDoctor = Doctor(firstName: "Sam", lastName: "Smith")
someDoctor.getFullName()
1 Answer
rh12
4,407 PointsHi Marina,
The reason has to do with inheritance. The super class "Person" has an init method that takes first and last name parameters. Since "Doctor" is a subclass, or is inheriting its characteristics from the "Person" class, you do not need to specify that particular init method again.
A sub-class inherits the following from their super class: Properties, Methods (including Initializers) and Subscripts.
Hope this helps.
Ryan
More information about inheritance (in Swift) can be found here: The Swift Programming Language - Inheritance