Hey, what's up everyone? Mark Price here at devslopes.com, and today, we're going to talk about inheritance. Not like the inheritance you got from your grandfather. When all your peers are out there working their butts off, failing and failing and failing again, and they're sweating and bleeding and you're out there and you just get an inheritance and you're just like here's my Bugatti, you guys suck. Yeah, it's not one of those kind of things, it's a little bit different. It's more so like an inheritance of features, more like I have red hair because my grandfather has red hair. You've inherited some type of features from some type of relative. So I'm going to get started with a playground. We're going to call this inheritance. Inheritance, it's one of those words that's easy to misspell. Inheritance, is a principle of object-oriented programming, object-oriented programming. So a perfect example is continuing on with a car or a vehicle. We've got a vehicle class, and a vehicle class has some basic features as well. So we'll go ahead and say like var tires equals four, you've seen something like this before, and then var make of type string, but right now there's nothing in it. Var model of type string, and we said func drive and then we can say func brake, and say var current speed of type, let's say double, and then I will start it off at zero, the car is not moving. So maybe, and let's say we're creating complex drive algorithm, where it's going to move, accelerate the car forward. So we pass in the speed increase of type double. So for a basic car, the basic algorithm is this, it is just current speed, plus equals speed increase times let's say two, let's say that was the algorithm, summed random arbitrary algorithm. The current speed of the vehicle equals the current speed plus speed increased times two. Now let's go ahead and create what's called a subclass. So this is a class, let's create a subclass. We're going to show inheritance here, so we're going to say class. What do I want to say? Class BMW, class BMW, or even better how about sports car? Just for fun, sports car inherits from vehicle. So you start inheritance by putting a colon here, and then specifying the type which is this, this is the type, the type or class that you want inherit from. Now, this is really cool, there's nothing in here, nothing at all. But you can still access the properties of the parent class. So on this sports car, tires are going to stay the same, but maybe we can set the make and model. So I don't have to recreate those, which is really cool, I can say make equals, and let's say BMW, and model equals just say, three series. So we got to make in a model. What we need to do here actually is create an initializer firs like so, and then in here we can set our make in our model like so, and see yelling at us because it wants us to override it. Yeah, so we need to call super init, super init just some boilerplate stuff you got to do in every class. So every single class has by default an initialization function, but you don't see it right here because we did not implement it. So let's implement it now on the parent class. So we've got an initializer on the parent class, and initializer here in the subclass. This is interesting, we're calling super init on the parent class, what does that even mean? Well, let's print this out, I am the parent, and then let's print this down here, I am the child. What I want to do is just create an instance of this class, pending that Xcode doesn't crash on us. It crashed. So what I want to do is create an instance of the sports car, so let car equals a new sports car. What's interesting, if it decides to load, I am the parent prints, and I am the child. Well, what's happening here? So we create this sports car, and then this initializer is called, and the override function says, "I want to override the parents initializer function, so it won't be called at all." But then we actually call it here, explicitly. So then that's going to go back up to the parent class and call whatever in it. So sometimes parent classes have setup or things like that. It needs to do so we want to call, it's typical to call the parent class, and then to create your own functionality, which is really cool. So here's our sports car, and awesome. Now, let's say our sports car need to drive faster than the standard algorithm. So what we can do is we can even override the functions, so I can type and drive, it's going to override it, and now what I could do instead of current speed being equal to this, I can say current speed plus equals speed increase times three. So this one is times two, but this one's times three, and now the child class we know that every car needs to have a drive function, and so that's great. We wouldn't want to create a bunch of different classes and re-implement this function every single time. So it's already pre-built in, we just wrote it once, and we overrode it here, which is great. Every vehicle needs to be able to drive. But, it's implementation could be different per vehicle, is a Ford F150 going to accelerate differently than a Ford Mustang? Yes, every car is going to have its own acceleration, therefore it would have its own code here in the functions that it's overriding, and of course you can override the properties as well too. So the whole point of this lesson we could go way down this level, hold, we're not going to. The whole point of this lesson to know is that you can create a parent class and children classes can inherit from that parent class and you can do more than one. So if we've got a sports car, we can say class truck inherits from vehicle. We could have done the exact same thing there, and then you know initializer, let's just copy what we got here, and let's make it really easy so it doesn't yell at us. We can get rid of these here, and then a trunk could have its own drive function, that speed increase, plus equals or no, excuse me, current speed plus equals speed increase without multiplying it by a multiplier. So anyway, now we've got two different classes here that both inherit from vehicle, they both have a drive function but their implementation is different. Why this is important is because it helps you create objects that can adapt to different situations. For instance, on Instagram you may have a parent class that has a specific base filter, but then you may have different filters like Valencia and other things that have their own algorithms that they need to implement. So rather than putting it all one giant class file, they could easily have just created a parent class which creates a default filter, and then they can override it and add some other functionality to it for those specific filters. Again all kinds of things you can do with classes, objects, and inheritance, but that's all you need to know for now you're going to learn about it more as you use it in iOS development. So Mark Price here at devslopes.com moving on and forward.