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

Sam Kearney
Sam Kearney
2,058 Points

Class Inheritance Code Challenge Help

I think the initialiser is what's throwing it off, but I'm not sure what's wrong. Anyone see the issue?

Thanks

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

class Car: Vehicle {
  let numberOfSeats: Int = 4

  init(withSeats seats: Int) {
    self.numberOfSeats = seats
  }
}

let someCar = Car(withSeats: 5)

1 Answer

Hey Sam, Got this answer from an older question on this challenge, but it passes and seems to explain pretty well. Looks like you were really close!

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

class Car: Vehicle {

    var numberOfSeats: Int = 4

    init(numbersOfSeats: Int) {

       // We first give values to "numberOfSeats
        self.numberOfSeats = numbersOfSeats

      // Then we call super init
        super.init(withDoors: 2, andWheels: 4)
    }
}

let someCar = Car(numbersOfSeats: 2)

Hope this helps!

Dylan

Sam Kearney
Sam Kearney
2,058 Points

I understand it much better now. Thank you!

why don't you add "doors" and "wheels" when you call super init?