Object Oriented Swift: Class Inheritance; Differentiating between Objects
Some of the content in the previous videos was confusing to say the least! I've looked over the 'Helper Method' part again and was actually going to do a separate blog. However, halfway through that I realised I just can't make sense of so many previous values and properties being used. It did seem to be a giant conceptual leap! Anyway, I can always look back over that again, when I get to a similar level of difficulty at some point. The main point of the 'Helper Methods' was that a function is created specifically to help with another one within a class/struct.
Inheritance
So far in the game we have, we haven't accounted for enemies getting harder. A stronger enemy needs to be created! Now, I remember the point of this - that a class will inherit the properties of another one. Structs cannot do this! Pasan shows that you can just create new classes, copy and paste the code then tweak the value of the life property. However, DRY - don't repeat yourself!
Overriding Properties
When creating an instance of SuperEnemy, we need to ensure that this has initial values - as it will have some differences. Again the initialiser aspect is confusing, but I'm getting used to use of 'self', 'super' etc. It just seems a bit pointless at the moment. It's one of those things that seems protracted and doesn't show clear results, but it seems to be essential for behind the scenes...
Inheritance
So far in the game we have, we haven't accounted for enemies getting harder. A stronger enemy needs to be created! Now, I remember the point of this - that a class will inherit the properties of another one. Structs cannot do this! Pasan shows that you can just create new classes, copy and paste the code then tweak the value of the life property. However, DRY - don't repeat yourself!
class SuperEnemy: Enemy {
}
That's much easier and more convenient! So the SuperEnemy is called the 'subclass'. The Enemy one now becomes a 'superclass'.
Overriding Properties
When creating an instance of SuperEnemy, we need to ensure that this has initial values - as it will have some differences. Again the initialiser aspect is confusing, but I'm getting used to use of 'self', 'super' etc. It just seems a bit pointless at the moment. It's one of those things that seems protracted and doesn't show clear results, but it seems to be essential for behind the scenes...
class Car: Vehicle {
var numberOfSeats: Int
override init(withDoors doors: Int, andWheels wheels: Int) {
self.numberOfSeats = 4
super.init(withDoors: doors, andWheels: wheels)
}
}
This was a really hard challenge! I needed some help with this one but did get close on my own first. The whole override/init/self/super is still a bit of a mystery. Before I finish this course, I will do some more work on this. However, reading the comments from other people doing this course, I'm not alone in finding it confusing. That's reassuring!
Overriding Methods
We cannot access stored properties without initialising tower or laser tower. The properties of the subclass (laserTower) need to have initial values before we do anything with the superclass. We have to call the init method. Initialise the subclass, then the parent (superclass). We can override the parent's initialiser.
class LaserTower: Tower {
override init(x: Int, y: Int) {
super.init(x: x, y: y)
}
}
So let's analyse this as I pause the video. The LaserTower is a subclass of Tower. I have to initialise it, in order to access the values. In this case, I have to use override before init, and put in the same as I did with the superclass/parent class. Then I need to use the super.init - for the superclass.
This applies to override functions too. All of the properties can be inherited from a class - it's very powerful!
Structs vs Classes
So when to use each one? Classes have inheritance whereas Structs don't. Another key difference is going to be explored...
struct User {
var fullName: String
var email: String
var age: Int
}
var someUser = User(fullName: "Terence Stamp", email: "t.stamp@yahoo.com", age: 47)
var anotherUser = someUser
anotherUser.email
someUser.email = "zazoo@yahoo.co.uk"
anotherUser.email
In the example above, changing the original value (for someUser) does not affect that of anotherUser! Doing another example - the same info but with a Class, the value does update! This must have something to do with value vs reference types...
Value vs Reference types
A class is a reference type rather than a value type. The distinction of when to use value (struct) or reference (class) is not clear at the moment. All this is to do with encapsulating data.
Knowing when to use a structure or a class...according to Pasan this is nothing to worry about for now.
He gives a recap of the syntax of structs and classes - each of these being objects.
Stored properties can take on default values but don't have to. They can be constants or variables. Initialization - structs have these done automatically but classes need this made explicit.
And that's the course complete! I won't lie - there is still a lot here I'm still trying to grasp, namely initialisers, overrides etc. But Pasan does state that there will be plenty of opportunities to put this into practice! I can also check/read up on something as I go along. I think that's now up to date! This means that everything from now on is new, which is exciting! From now on, I'm going to focus on just a bit at a time, and seek other reading/challenges along the way where necessary...
Comments
Post a Comment