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

Assuming Doctor is a subclass of superclass Person, how do I override the initializer method, and how do I call the func

Really not having fun with these code challenges. It seems like I'm watching a lot of video, but failing to translate the skills explained into the very basic code challenges at the end.

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 {

    override init(firstName: String, lastName: String) {
        super.init(firstName: "Dr", lastName: lastName)
    }
}

let someDoctor = Doctor(firstName: "Dr", lastName: "smith")

2 Answers

OK, a few changes. First, you were to override the function, not the default initializer. Second, it was to return "Dr. " followed by lastName. Third, the first name was to be "Sam", not "Dr.".

class Doctor: Person {

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

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

Yea, it's gets confusing. I know!

this portion is KILLING me :'(, do you know of any resources I can use to help me out while practicing besides the apple notation (it's a bit broad)

Here's one: http://www.iphonelife.com/blog/31369/swift-programming-101-inheritance-polymorphism

If you google swift override or swift override class method you'll see others.