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

Michael Bates
Michael Bates
13,344 Points

Trouble understanding how to override methods...

I am having trouble understanding how to override methods. The quick error message I am receiving when checking my work is as follows:

"You didn't add the incrementBy method to the class RoundButton or click Preview for compiler errors."

What is the solution and reasoning behind it? Thank you in advance!!

Button.swift
class Button {
  var width: Double
  var height: Double

  init(width:Double, height:Double){
    self.width = width
    self.height = height
  }

  func incrementBy(points: Double){
    width += points
    height += points
  }
}

class RoundButton: Button {
    override func incrementBy(points: Double = 7.0) -> Double {
    }
    var cornerRadius: Double = 5.0
}

1 Answer

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

In your Button class, the incrementBy function returns void, as you have not specified any return type. In your RoundButton class, however, incrementBy will return a Double. Therefore, these are two different functions and thus you are not overriding it, but creating a new one. Removing -> Double from your RoundButton incrementBy function should override as expected.

Hope that helps :)