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 Creating a Subclass

william fashanu
william fashanu
2,108 Points

i'm struggling in creating a sub class

class Vehicle { var numberOfDoors: Int var numberOfWheels: Int

init(withDoors doors: Int, andWheels wheels: Int) {
    self.numberOfDoors = doors
    self.numberOfWheels = wheels
}

}

i dont know how the overide init and the super.init work and what is meant to go into them

classes.swift
class Vehicle {
    var numberOfDoors: Int
    var numberOfWheels: Int

    init(withDoors doors: Int, andWheels wheels: Int) {
        self.numberOfDoors = doors
        self.numberOfWheels = wheels
    }
}

// Enter your code below

4 Answers

Bruno Junqueira
Bruno Junqueira
31,740 Points

William,

In this code challenge, you don't need to override the init method. Just create the subclass and add the property that the exercise asks for.

class Car: Vehicle {
  var numberOfSeats: Int = 4
}
let someCar = Car(withDoors: 4, andWheels: 4)

The propose of the init is to initialize the class with values passed to the class as arguments. And the super.init is to initialize the Super Class. The one that the subclass is inheriting.

william fashanu
william fashanu
2,108 Points

when adding a new property to the sub class, how do you initialise that?

Lucas O
Lucas O
3,714 Points

Why does this not need an initializer method?