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

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