Locked lesson.
About this lesson
Using other classes inside of your class.
Exercise files
Download this lesson’s related exercise files.
Class Inheritance.docx60.9 KB Class Inheritance - Solution.docx
60.2 KB
Quick reference
Class Inheritance
Class inheritance allows us to use other classes inside out classes.
When to use
Use this whenever you want to use something from one class in another class.
Instructions
Inheriting another class is pretty easy:
class Hired(Employee):
pass
That will allow the Hired class to inherit everything from the Employee class.
You can now access everything from the Employee class in your Hired instance.
Note: when instantiating a Hired object, you still need to pass whatever parameters that the Employee class requires!
Hints & tips
- Inheritance allows us to use other classes inside of other classes
- Just name the class you want to inherit from when creating your class
- class Hired(Employee): # now the Hired Class Inherits everything from the Employee Class
- 00:04 In this video, I want to talk about class inheritance.
- 00:07 So we've created one class.
- 00:09 It's the employee class, but you can create lots of different classes and
- 00:13 you will.
- 00:13 And a lot of times, you want these classes to interact between themselves to access
- 00:18 themselves and be able to do things from other classes inside of other classes.
- 00:23 And to do that, you use class inherited and that's what we're going to talk about.
- 00:26 So this will be our main class, our employee class, right?
- 00:29 So let's create another class and let's just call it class, let's call it Hired.
- 00:34 And normally, we would go like that just like we did up here.
- 00:37 All right, but in this case,
- 00:39 we want to inherit all the stuff from the employee class.
- 00:43 We want to be able to access all this good stuff inside of our Hired class.
- 00:47 So it's really easy to do that.
- 00:49 We just type in inside of this parenthesis and you can see our program already knows
- 00:54 we're going to ask to do this employee, because it's the only other class.
- 00:58 So we can start to type employee and hit tab and it'll put it right in there.
- 01:02 Now, we could just type a pass in there and
- 01:05 then we can access all of the stuff inside the employee class.
- 01:10 So normally when you create a class, you do this Initialization thing.
- 01:14 But in this case, I don't want to do that,
- 01:16 because I'm going to use all the stuff in this class.
- 01:19 So I could just kind of skip that, but I do want to create,
- 01:24 let's create a Hired class and call it self.
- 01:28 And then let's see, what do we want this to do?
- 01:31 Let's return, just return a string that says, ("You're hired!").
- 01:37 All right, we save this.
- 01:38 Now, we can come down here and let's create an instance of our Hired class.
- 01:43 So we can say, hire_me = and then we call the class name Hired()
- 01:48 which you notice right off the bat, we're getting an error, right?
- 01:53 Well, we're inheriting from the employee class and
- 01:57 the employee class requires a bunch of variables.
- 02:00 A bunch of arguments, a bunch of parameters, four of them, in fact, and
- 02:04 we haven't listed any.
- 02:06 So you have to be sure to create, to pass in the correct number of parameters.
- 02:12 Let's really quickly just go ahead and do this.
- 02:15 I'm just going to go me@me.com.
- 02:17 Salary of 300,000, save this.
- 02:20 Now if we run this, of course, still nothing's going to happen.
- 02:25 We just created an instance, but we haven't done anything with our instance.
- 02:27 So we can go, let's go print hire_me.
- 02:32 Let's do this hired method, .hired.
- 02:35 Forget our parenthesis.
- 02:38 So now if we run this, we get you're hired, right?
- 02:42 So okay, that's just what we would expect to happen.
- 02:45 We created an instance.
- 02:46 Here's our class, we ran the hired method printed this out, okay?
- 02:51 So what?
- 02:52 We've also inherited an employees.
- 02:54 So now inside of this instance, we can access all of these things as well.
- 02:59 So we could pull hire_me.report, you save this and run it.
- 03:04 We get John Elder me@me.com $300,000
- 03:07 even though this is an instance of the Hired class.
- 03:11 And inside the Hired class, there's nothing to do with a report.
- 03:15 There's no mention of anything report related.
- 03:18 But since we inherited everything from the employee class, we can access all
- 03:22 this stuff inside of this hire me instance and then it's just really, really cool.
- 03:27 So that is inheritance, super easy, not much to it.
- 03:30 We just slap in the name of the class we want to inherit from.
- 03:34 And really, the big thing to keep in mind is when you create your instance, you have
- 03:39 to pass in the same number of parameters that the class you're inheriting requires.
- 03:43 In this case, its first name, last name email and pay.
- 03:46 Just keep that in mind or remember to do that and you'll be good to go.
- 03:48 So that is class inheritance and that is the end of our advanced Python section.
- 03:54 And so also the end of the entire course, I hope you enjoyed this.
- 03:57 Python is just a great language.
- 03:59 It's so much fun to use and so easy, and I hope you've sort of got excited.
- 04:04 You just sort of scratched the surface of what you can learn with Python.
- 04:07 There's so much more to learn and I hope you keep learning, and growing.
- 04:10 So I hope you enjoyed the course.
- 04:11 My name is John Elder and thanks for watching.
Lesson notes are only available for subscribers.