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 trialMonzer Selim
Courses Plus Student 3,849 PointsMy question here how can i inititate a new property in the subclass plus overriding the init method?
My question here how can i inititate a new property(that i didn't provide value for in first place ) in the subclass plus overriding the init method to change some properties in the superclass
1 Answer
Edgar Barajas
6,906 PointsI did it this way... Basically, remove the "override" keyword from subclass init method, give the property a value from the arguments...
class Enemy { var life: Int = 2 let position: Point
init(x: Int, y: Int) {
self.position = Point(x: x, y: y)
}
func decreaseLife(by factor: Int) {
life -= factor
}
}
class SuperEnemy: Enemy { let isSuper: Bool
init(x: Int, y: Int, isSuper: Bool) {
self.isSuper = isSuper
super.init(x: x, y: y)
self.life = 50
}
}
let superEnemy = SuperEnemy(x: 3, y: 2, isSuper: true)