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 trialJonas Kristensen
2,738 PointsOverriding Methods challenge
Our RoundButton class should have its own implementation of the incrementBy method. Override the incrementBy method in the RoundButton class and default the points parameter to 7.0. Any help?
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: 7.0) -> Double{
width += points
height += points
return super.incrementBy(points)
}
var cornerRadius: Double = 5.0
}
1 Answer
Jhoan Arango
14,575 PointsHello : Jonas Kristensen
I am going to paste your code, and below it, I will past the answer and give you notes within both to better explain.
Your Code:
class Button {
var width: Double
var height: Double
init(width:Double, height:Double){
self.width = width
self.height = height
}
func incrementBy(points: Double){ // Method Signature
width += points
height += points
}
}
class RoundButton: Button {
override func incrementBy(points: 7.0) -> Double{ // Method Signature not the same as superclass, this one has a return type, when the super class signature does not have a return type.
width += points
height += points
return super.incrementBy(points) // Not needed
}
var cornerRadius: Double = 5.0
}
Answer to Challenge
class Button {
var width: Double
var height: Double
init(width:Double, height:Double){
self.width = width
self.height = height
}
func incrementBy(points: Double){ // Method signature
width += points
height += points
}
}
// On your RoundButton Class you give a default value by first giving it a type, then it’s value
// Also since the name of the parameter “points” is optional, you have to add an underscore.
// otherwise you would have to explicitly give it a name in its super class: (#points: Double). This won’t work in the challenge tho.
// also remember that in order to override a method, it’s method signature must be the same.
class RoundButton: Button {
override func incrementBy(_ points: Double = 7.0){ // Method signature
width += points
height += points
}
var cornerRadius: Double = 5.0
}
Hope this clears any doubts you may have, any other questions please ask :)
Happy learning
Jonas Kristensen
2,738 PointsJonas Kristensen
2,738 PointsThansk a lot!!!!