Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?