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

Method Overriding Challenge 1 of 1

Hey guys,

This question asked to create an override of RoundButton to the base class - Button. I have added an override function but the output tells me that there's a problem with my original func where I cannot use += for my width and height. I've looked at the teacher's notes but I'm still lost. Pls help. Thanks.

Here are my codes:

class Button { var width: Double var height: Double

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

func incrementBy(points: Double) -> Double { var width += points var height += points return (width, height) } }

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

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) -> Double {
    var width += points
    var height += points
    return (width, height)
  }
}

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

1 Answer

Dominic Bryan
Dominic Bryan
14,452 Points

HI Nick Chew, You have the right idea but the function/method of incrementBy never overrides because the method signatures are not the same. You are changing the method increment by to return a Double, " -> Double " which is throwing you the error.

Simply delete that out and your method signature is the same and there you have it. So you were pretty much right just one small thing ( I think it might be because in the previous video to this question, amit's method returns a Double so you may be confused with that) Here is the code:

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 {
  var cornerRadius: Double = 5.0

   override func incrementBy(_ points: Double = 7.0) {
    return super.incrementBy(points)
  }

}

Hope this helps

Dom