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 trialBerry Loeffen
4,303 PointsSuperEnemy without a default property
Building a SuperEnemy and LaserTower looks easy to do. However if the SuperEnemy would nog have a property with a default value? I've been trying a lot and this is what I came up with. However I don't know if this is the correct way to do this. I've used isSuper: Bool as a property without adding a default value:
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
}
}
It compiles fine but is this the correct answer to my question? In this way we first initialize SuperEnemy, without using override. Then we initialize the stored property of SuperEnemy. Then we initialize the superclass which is Enemy, and then we can adjust the value of life.
Curtis Rickard
6,610 PointsHey Berry ,
How might I contact you to discuss business?
2 Answers
miikis
44,957 PointsHey Berry,
You wouldn't add an isSuper property to SuperEnemy — that characteristic would be self-evident due to what you've named the subclass: SuperEnemy. Instead, you would ask yourself what makes a SuperEnemy different from a regular Enemy. And whatever you come up with — maybe SuperEnemy starts out with 50 Life Points and regular Enemy starts out with 25 — that's what you would change/override with your subclass.
I'm not sure what you mean about default values; I think you're referring to the overridden properties from the super class... but it's unclear. If you're asking about adding brand new properties to SuperEnemy that Enemy never had, you should keep in mind the SOLID principles of Object-Oriented Design. The L in SOLID stands for the Liskov-Substition Principle which essentially advises that you can change SuperEnemy however you want as long you could, theoretically, replace it with it's base class, Enemy, and your app would still work the same (read: it wouldn't break).
Let me know if that answers your question.
Update
Just saw your second question about initializers. And yes, you only use the override keyword to override an initializer with a given method signature that already exists in the base class. Keep in mind that a Swift class can have more than one designated initializer and that some can be marked required.
Berry Loeffen
4,303 PointsThnx Mikis.
This answers the question. Was just playing around to see how things work.
Berry Loeffen
4,303 PointsHi guys,
Question is if this is the right way to inherit if there is a new stored property without a default value. Even though this compiles fine in xCode, just want to know if this is the right way to go.
In this course there is no teaching material provided if there is a new stored property without a default value. Making it not very clear what to do when things are different as in the video's.
Another question: Do you only use override init when the initializer of the superclass is the same as the subclass ( or subclass has a new stored property but with a default value)?
Thanks!
Paul Karim
3,428 PointsHey barry! I've been asking myself the same questions. However I do believe these questions and more will be provided as we advance in the class. If you figured it out already I'd be glad to hear your findings though.
miikis
44,957 Pointsmiikis
44,957 PointsHi Berry, You haven't asked a question — what is your question?