Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.4 Inheritance

In this lesson you are going to learn about inheritance and how it pertains to Swift. At a high level, inheritance is what allows us to reduce duplication within our code by giving classes the ability to use functionality defined in our classes. Confused? Not to worry. We've got you covered.

3.4 Inheritance

The next important principle of object-oriented programming is going to be the idea of inheritance. Now inheritance is a very nice feature that's going to allow you to save some time and effort and not force you to duplicate a lot of code. Now the basic concept behind inheritance is that I can create a hierarchy of classes that can inherit functionality from each other. And then ultimately save ourselves from doing a lot of that duplicate code. So a very common example when you're working with inheritance is the idea of creating some sort of structure or series of classes that are going to model the workforce for a company. So let's say we're gonna have managers, we're gonna have employees, and we're also going to have some common, some commonality or some common functionality between the two. So let's start off by seeing how we would model this type of thing very simply within Swift. Well, at the end of the day, an employee and a manager share certain characteristics because they are people, at the end of the day. So let's create a class to begin with called person. Now, because we are starting to introduce the concept of inheritance, you are now restricted to the use of classes. Structures do not support inheritances because of their, their nature and their data type. So, you're going to need to use classes in order to follow along for the rest of this concept for inheritance. Now, we're gonna create a person and we're gonna give it some properties. We'll say that this person has a firstName. That's gonna be a string and that it's also going to have a lastName, that's a string. Now at this point, it bears noting that I can absolutely define properties this way. If I do define them this way but don't initialize them then I have to provide an initialization method that's going to handle that for me. And that's exactly what this little red dot is telling me right here, it doesn't have any initializers. So I could either just default these properties to be empty strings or I can provide an initializer. But for illustrative purposes, I'm gonna provide an initializer and it's gonna become interesting, and you'll see why very shortly. So I'm gonna provide two inputs to this, an fname and an lname, which are going to be strings, which are ultimately going to be used to initialize firstName, as well as lastName. So there you go, now we have a class person that represents a normal everyday person that has a first name and a last name. Now the next rung on the hierarchy is gonna be the concept of an employee. So let's create another class that's going to model the employee and this person is going to have maybe a jobFunction which is also gonna be a string for simplicity's sake. And for this case, we're just gonna initialize this to be something else. So we're just gonna say this person is gonna be a developer. And then obviously, this person can also, or this employee is gonna have some function to do within their job so we can create a method here, and we'll call this DoWork. And you can make this as complicated or as simple as possible for, but for in this case, we're going to simply return a string just so we know that this person's actually doing something, and we're going to return I'm doing my work. There you go. So now we have two classes, an employee and a person that represents some functionality that's existent in the real world. Well now I can go ahead and I can come in and create a new employee. So we'll say employee one is going to be a new employee. And then this employee, I can access the jobFunction as well as the DoWork method here. So I'll go ahead and do that and you can see this person is doing their work. So that's very nice, but what happens if I need to access, for this employee, their first name and last name, maybe for payroll or for tax purposes or whatever have you? I don't wanna have to copy this information and then duplicate it in here because then I have to maintain and even more code. So, the way around that is to use inheritance. So, to do that, I'm merely gonna come up to my employee class declaration here, and I'm going to, just like I did for protocols, I'm gonna specify a colon and then I'm going to define this as being a person. Now this is going to cause a little bit of a breakage here and I'll show you why in a minute. But the nice thing about this is by simply adding this on the tail end of your declaration of your class, you now get access to the functionality that is found within person. But with that, you're going to change some of your implementation. So as you can see here, I got an error here. Because I'm missing arguments, or missing an argument for parameter fname in my call. So, what happened now because I inherited person from employee. If I were to look at the initializer here, I now get the initializer that was defined within this person class. So that's kinda nice. So now I can specify the first name as being Derek, and I'll specify the last name as being Jensen. Now, if I were to come down and dig deeper into this employee object as employee one, not only do I have the DoWork method and the jobFunction, I also now have a firstName and a lastName. So I can get access to all of those things that were exposed to me in this person class. So I can take a look at firstName. I can also come in to employee one and take a look at lastName. So now, I have this concept of inheritance, where I'm inheriting the person functionality in employee. Well, let's say we keep going. Let's say we have a manager, so we'll create a manager class. And a manager is also going to be an employee. So I could, say that this is going to be a person. And then I could add on, on all the functionality, over and above, from person. But really, a manager is still gonna have a job function, just like an employee does. And a manager is going to have to do work. So now I can create multiple levels of inheritance, where I can have manager inherent from employee who emplo, who inherits from person so I can create this very large hierarchy of objects that are going to allow me to share all of that functionality across the, across the way. So now let's say that a manager is actually going to have employees, or, or maybe direct reports, we'll say directReports. And that's actually going to be an array of employee, and the way that we're gonna define an array of emplo, employee is going to be using this shorthand notation of square brackets and the type of the objects within the array. And we're gonna talk about arrays a little bit more in an upcoming lesson, but just take my word for it at this point. And then we'll just initialize it to be an empty array, just to get past the little error there that was saying that I'm gonna have to use an initializer. So now I have this concept of classes and inheritance that's going to allow me to save myself a lot of duplication. So, now I can create a new instance of a manager. We'll say manager one is gonna be equal to a manager. And, as you'll see here, I'm also inheriting from the initializer that's two levels deep. So, now I can come in here and say John Doe. Like that. And I can look at manager, manager one and I can see that I have a jobFunction, I have firstName and lastName, I can do work, and I also have a series of direct reports. So I could come into direct reports and since this is an array, I have access to an append method and I can add in here that employee one is actually going to be a direct report of manager. And now I have the concept of a manager having direct reports. And one of the direct reports is actually employee one that I've defined up here as being myself. So as you can see, I can start to create these very rich hierarchies of objects and be able to restrict the amount of duplication that I'm creating within my code, and at the same time, create some very nice feature-rich classes to use within my application.

Back to the top