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
Cory Sovilla
11,087 PointsOverriding Init Methods & Best Practice Help
If I were to remove self.life = 50 from SuperEnemy would it be best practice to remove the init method all together due to Enemy and SuperEnemy being the same Init method? Also, is overriding an Init method only necessary if I change it? In what cases would I not need the override keyword.
class Enemy
{
var life: Int = 10
let position: Point
init(x: Int, y: Int)
{
self.position = Point(x: x, y: y)
}
func decreaseHealth(factor: Int){
life -= factor
}
}
class SuperEnemy: Enemy
{
let isSuper: Bool = true
override init(x: Int, y: Int)
{
super.init(x: x, y: y)
self.life = 50
}
}
Sorry for the mess I don't know how to force a snippet on a set of words it auto does it.
1 Answer
David Yoo
6,889 PointsHi Cory,
An override would be unnecessary in this case. Normally an override would probably be necessary in a subclass since you're changing something from the superclass.
David