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 Inheritance Overriding Methods

Not sure exactly what the second challenge in overriding methods is asking?

I find it really frustrating that the challenge questions ask you to do something within code that is very different from the code used to teach in the prev video.

3 Answers

Hey JD, I can imagine how frustrating the concept of overriding methods can be (it confused me for quite some time as well).

Basically, the challenge is asking you to override the Button class's incrementBy method in the implementation of Button's subclass RoundButton. I'll help explain how to do so because its such a powerful tool to know how to use in all your future coding endeavors once you get it =)

class RoundButton: Button {
    var cornerRadius: Double = 5.0
}
  • Implement the Button class's incrementBy method within the RoundButton subclass following the "override" keyword.
class RoundButton: Button {
  override func incrementBy(points: Double) {
    width += points
    height += points
  }
    var cornerRadius: Double = 5.0
}
  • Give the points parameter of the overriden method a default value.
class RoundButton: Button {
  override func incrementBy(points: Double = 7.0) {
    width += points
    height += points
  }
    var cornerRadius: Double = 5.0
}
  • Specify that the parameter is now optional as it now has a default value with an underscore.
class RoundButton: Button {
  override func incrementBy(_ points: Double = 7.0) {
    width += points
    height += points
  }
    var cornerRadius: Double = 5.0
}

I hope this helps clarify the challenge for you, overriding methods almost always follow this kind of pattern so feel free to use this answer as a reference point. Keep practicing and try overriding your own methods for classes that you created. This concept takes a little while to grasp (as it did with me), but with a little experimentation it will start to become second nature. Good luck and happy coding!

Thank You so much for your help Alvin, i truly appreciate it. Yes it did help a lot.

I'm glad it helped, if you ever come across anything in iOS that you need help clarifying feel free to ask me, and I'll do my best to help you out. Cheers!

Thanks Alvin.

Piers FC
Piers FC
10,402 Points

Hi Alvin, this answer no longer seems to work and is really frustrating.

Any idea why it is no longer working?

May be a bug in Treehouse