hey everybody how's it going in this video we'll be learning about python class inheritance now just like it sounds inheritance allows us to inherit attributes and methods from a parent class now this is useful because we can create subclasses and get all of the functionality of our parent class and then we can overwrite or add completely new functionality without affecting the parent class in any way so let's go ahead and look at an example of this and we'll see why this is useful so for our object-oriented video so far we've been working with this employee class now let's say that we wanted to get a little more specific here and create different types of employees so for example let's say that we wanted to create developers and managers now these would be good candidates for sub classes because both developers and managers are going to have names email addresses and a salary and those are all things that our employee class already has so instead of copying all this code into our developer and manager subclasses we can just reuse that code by inheriting from employee so let's go ahead and create these developer and manager subclasses and it's just as easy as creating a new class just like we did up here with employee but we're going to call this new class developer and after the name of the class we can put these parentheses here and specify what classes that we want to inherit from so in this case we want to inherit from the employee class now I'm just going to put in this past statement here for now because I want to show you that by simply inheriting from that employee class that we inherited all of its functionality so right now even without any code of its own the developer class will have all of the attributes and methods of our employee class so right now down here I have two instances of our employee class and then I'm printing out both of their emails so if I save this and run it you can see that when we create two new employees and print out their emails that we get this result so now instead of creating two new employees I'm now going to create two new developers and pass in all of the same information so now if I rerun this and print out those emails you can see that those two developers were created successfully and that we can access the attributes that were actually set in our parent employee class so what happened here is that when we instantiated our developers it first looked in our developer class for our init method and it's not going to find it within our developer class because it's currently empty so what python is going to do then is walk up this chain of inheritance until it finds what it's looking for now this chain is called the method resolution order now I want to show you this really useful function here that makes these things a lot easier to visualize and that is the help function so first I'm going to copy out these two lines here and then I'm just going to print out this help function and I'm going to pass in the developer class so if I go ahead ahead and run that and I'm going to make this a little bit bigger here now you can see that when we run help on that developer class that we get all kinds of good information here so that method resolution order that I mentioned is one of the first things that gets printed out and basically these are the places that python searches for attributes and methods so when we created our two new developers here it first looked in our developer class for the inip method and when it didn't find it there there then it went up to the employee class and it found it there so that's where it was executed now if it hadn't found it in our employee class then the last place that it would have looked is this object class and every class in Python inherits from this base object now if we look at this output further then it actually shows the methods that were inherited from employee so you can see here that we have the inip method and we also have our apply Rays method and our full name method and if I keep scrolling down here uh then you can also see that we have our data and other attributes and you can see that the class attribute raise amount uh was also inherited from the employee class so we got all of this code for free just by inheriting from that employee class okay so now I'm going to go ahead and make this smaller again and I'm going to take out that printed help statement okay so now let's say that we wanted to customize our subass a little bit now I'm going to make a very simple oneline change in here and I'm just going to change the raise amount um but first let's go ahead and see what uh happens when we and apply arrays on our current developer so I'm going to print out this should actually be pay and this should be pay now I'm going to print out uh our current developers pay here which should be 50,000 then I'm going to apply a raise and it should find our employees raise amount of 4% and then I'm I'm going to reprint out that developers pay again so if I save that and run it you can see that it printed out 50,000 applied the raise and then uh printed out 4% higher but let's say that we wanted our developers to have a raise amount of 10% now to change that it's just as easy as coming into our developer class here and changing the raise amount to 10% so now if I go ahead and rerun this you can see that it used our developer classes raise amount instead of our employee classes raise amount now if I was to change this instance back to an employee instead of a developer and then reran this then you can see that now it's back to that employee 4% amount so the thing to take away here is that by changing the raise amount in our subass it didn't have any effect on any of our employee instances so they still have that raise amount of 4% so we can make these changes to our sub classes without worrying about breaking anything in the parent class okay so now I'm going to go ahead and change this back to a developer and we'll make a few more uh more complicated changes so sometimes we want to initiate our sub classes with more information than our parent class can handle so what do I mean by that so let's say that when we created our developers here that we wanted to also pass in their main programming language as an attribute but currently our employee class only accepts first name last name and pay so if we also wanted to pass in a programming language there then to get around this we're going to have to give the developer class its own AIP method so what I'm going to do is I'm just going to go up here to our employee class and grab that inip method and I'm going to paste it here within the developer class now along with the first name last name and pay I'm also going to add in an argument here for the programming language now what you might be tempted to do here is just go up and copy all of this code from our employees classes and nit method and paste it into our developer classes AIT method but we don't want to do that because we want to keep our code dry and not repeat this logic in multiple places because we want it to be as maintainable as possible so instead of copying and pasted pasting that uh what we're instead going to do is just let our employees and nit method handle the first name last name and pay and then we'll let the developer set the programming language so in order to let that employee handle the first name last name and pay what we can do here is just do super do and nit and then we can pass in the first last and pay so again super. anit is going to pass first last and pay to our employees a nit method and let that class handle those arguments now there's multiple ways of doing this logic here you may have seen some people do employee. anit and instead of passing in first last and pay they'll type in self and then first last and pay now both of these ways of calling the parents and nit method will work but I tend to use super because with single inheritance like we are using here it's a little bit more maintainable but it's really necessary once you start using multiple inheritance and we're going to go over that in a future video but to keep things simple I usually just like to always stick with super so now that we're letting our employee classes ENT method handle the first last and pay uh now we can handle the programming language argument just like we would in any other class so I can just say self.
programming language equals the programming language that we passed in here okay and that should be all we need for our anip method so now when we instantiate our developers down here it's also going to be expecting a programming language to be passed in so I'm just going to go ahead and pass in Python for our first developer and I'll pass in Java for our second developer so now to make sure that this worked I'm going to comment out those lines there and I'm going to print out the first Developers email and I'm also going to print out the first developers programming language okay so if I run that then you can see that both of those were set correctly so we got the email set by it uh when we passed in all of our arguments here it came up here and it ran our employees AIT method and set all of those within there and then it also set our programming language uh within our developers in nit method there so you can see why this subclassing is us useful because we were able to customize just a little bit of code and we got all of this code from our employee class for free just by adding in that one little line there okay so just so we can get a really good understanding of this let's go through the process of creating another subass called manager and I'll go through all these steps again but I'll go a little bit faster this time okay so right here below our developer I'm going to create a another class and I'm going to call this class manager and this is also going to inherit from employee now when I create a new manager I'm going to give the option of passing in a list of employees that this manager supervises so we're going to need to add an AIT method for our manager and instead of typing all this in I'm just going to grab this init method here from our developer and paste this in here okay but this is going to be a little bit different instead of a programming language for our manager I'm going to let them pass in a list of employees and I'm going to set the default to none and then instead of setting this programming language here I'm going to say if employees is none self. employees is equal to an empty list and then else self. employees equals employees now you might be wondering why I didn't just pass in an empty list as the def default argument here instead of none but you never want to pass mutable data types like a list or a dictionary as default arguments and that's a topic for another video and I plan on doing one on that soon uh but for now we'll just go ahead and set our employees to an empty list if the argument is not provided and set them equal to that employees list if it is okay so now let's add in a few methods here so I'm going to give the option to add and remove from our list of employees that our manager supervises and to do this I'll add in a method called add employee and add employee will take self just like all of our uh instance methods do and employee and then I'll just say if the employee is not in self.
employees then I will just append that employee to our list so self. employees. append that employee and now I'm also going to create another method here to remove employees from this list and it's going to be similar so I'm just going to go ahead and copy that but this is going to be remove and we'll say if the employee is in our list of employees then remove that employee okay and lastly I'm going to add a method that will print out all of the employees that this manager supervises so I'm going to call this uh method print employees and this isn't going to take any more arguments than just self and I'll say for employee in self.
employees and then I will just print out that employee and before the employee I'll go ahead and put an arrow here just so it sticks out a little bit further and instead of just printing the employee I'll actually print out the employee full name okay so I think that we are finished with our manager class so we have our own NP method here which it'll accept a first name last name pay and also a list of employees that this manager supervises and then we have the ability to add employees to that list remove employees from that list and to print out all the employees from that list so now let's see if this works so I'm just going to comment out these lines here and now I'm going to create a new manager and I'll call this manager one so now I want this to be a manager and for the first name I'll do sue for the last name I'll do Smith we'll say that the pay is 9,000 and let's say that she supervises this first developer here so now let's make sure that this manager was successfully created and that we have uh all of the attributes and methods available uh that it would have inherited from that employee class so let's go ahead and print out this manager's email address so now if I go ahead and print this out you can see that the email address was set correctly but now we also added in all of this extra functionality so let's see if we can print out all the employees that this manager supervises so I'll do manager. print employees and if I go ahead and run that then you can see that it prints out the full name of the one employee that they currently supervise and I can and add to that list of employees so here I'll just say manager one.