 Welcome back everyone. This is Brian and we're going to continue our journey with Python 3 with class inheritance now when we talk about inheritance We're not talking about a loved one dying and leaving us everything they own We're talking about how a class can inherit or include the attributes of other classes So for example, you were born into this world. You have a mother and a father You have a hair color and an eye color and those came from your mother and father you inherited those traits That's really what we're talking about with classes. This can get super complex and we're gonna look at a few issues that can pop up Okay, we're gonna go through a lot of this at warp speed just because we've covered this before so I don't want to waste a lot of your time We're gonna make a feline class. Now. Remember Class is just a blueprint I'll say feline and Then I'm going to go ahead and define our constructor Self and let's give it a name There we go Notice one thing right off the bat. We're saying self name, but we've never actually declared a name variable Pay special attention to that because we're gonna find out really really hard how that's a bad idea And that's why I said I tie I try to declare all my variables at the top that way I know instantly what the class has and it's been declared. This is gonna come back and haunt us And I'm going to say Creating a Line so we have a constructor here Now I'm going to just do speed this up just a bit I'm going to put some functions in the feline class It can meow and we can set the name So really all we're doing is meow just prints out self name meow and set name you guessed it prints out setting the name and Then we go ahead and set that variable Remember we haven't declared this Now I want to make another class that inherits the feline class You know how when I mouse over this it says feline feline. So it's just a self-contained class with no inheritance We're going to make a class and I need a snazzy name. Let me get my notes here. We're gonna make the lion class I'm gonna say class lion And you may have been wondering why classes would even have a parentheses here. Well, we're about to find out The line That's all we're doing here is we're saying the lion Will use everything in the parentheses and the first thing it's going to use is the feline class That is just simply how we inherit Now we say deaf Roar And we have to have the self in there and we can just print something out. There we go Looks super super simple and well, let's go ahead and play around a little bit here with the code So we can say c equals feline And we got to give it a name. So let's call this kitty Go ahead and print c out and let's say see meow go ahead and run this and it says creating a feline so our constructor of our feline class was called and we have a Main notice how it's got the underscore underscore main. So we're at the main part meaning this is python at the root It's created a feline object at this memory location and the kitty is meowing Now let's go ahead and make a lion. So I'm gonna say L equals a lion And let's call this Leo Go ahead and print out that object so we can see what's going on under the hood and Let's also say L dot meow now. You notice something magical just happened The lion class does not have a meow function. It is inheriting it from the feline So in short everything you inherit you now have access to This is extremely cool And we can also add to this class. So we're saying the lion is not just a feline All of this class all this blueprint, but also this additional stuff that we can define. So now we can say like L dot Roar Go ahead and run this and sure enough Creating a feline, you notice how the constructor for the feline was called and then the lion is created Interesting how that works. We did not define a constructor for lion. So we're seeing the default constructor for the feline Interesting that is gonna come back and haunt us here in the next section I keep saying we're gonna have a bad time and that's coming right up, but I want you to understand what's going on here Python is creating a feline and creating a lion those two are together into one object at this location But it's simply going to say main dot lion now the main of course is this file right here and Then Leo's gonna meow and Leo's gonna roar and if I just Go up a little bit. You can see the kiddies meowing the lion's meowing the lion's roaring So everything's working as expected now fasten your seatbelts because things are about to get really really bumpy Let's go ahead and make another class here And we're gonna call this the tiger class because we are going to inherit the feline class Almost a timer that would have been interesting All right, so we've got our feline class now one thing I'm going to do here is I'm gonna go up here and just grab this and I'm drawing your attention to the default constructor I shouldn't say default the constructor for our feline class where we have a self and a name and remember I said we did not initialize that variable. We are doing it right here in this constructor But what I'm gonna do now is I'm gonna go to this tiger class and you guessed it plop that in here I'm gonna get rid of the name because guess what we're inheriting the feline has a name So I don't really want to mess with it with the tiger So now I have a different constructor even though What you can see is we are constructing a feline. So bad things are about to happen All right, let's go ahead and Say Instructing a tiger I want to plop some notes in here And they may not make a lot of sense at the moment Super allows us to access the parent when I say the parent we're talking about this right here Whatever we are inheriting from or the base class if you will if we forget This we'll have a bad time later meaning we can say super the parent in this case the feline Call the constructor with a name and we're just gonna set no name now. I'm gonna comment that out And we're just gonna say creating a tiger Oh boy, we've got some problems here. So now let's go ahead and make another function And we're gonna say stop I'm trying to kind of like explain this as we go. So you understand why this fails because it will fail What we're doing here Is we are trying to rely on python to do all of this management for us And we have to make sure That name is set in the parent when we say parent. We're talking about the super or the base class This is considered look before you leap. We are dynamically adding the attribute and what do I mean by that dynamically adding the attribute Right here We're adding it in the constructor of the feline if we bypass that constructor Using our own in the tiger and we don't call the constructor of the feline class That variable never exists. What happens when you call a variable? It doesn't exist. You get a not defined error So let's go ahead and print And we're gonna do a formatted print and we're gonna say Self not name Notice how IntelliSense is saying it's valid because we're working with a blueprint not an object And to make things even worse We think this is gonna work Let's go ahead and pop in yet another function Let's say I want to be able to rename this We're going to say super because we know there's a concept of a super which we're actually accessing the feline class And we're going to call the feline class set name In case you're wondering, yes, we could actually call tiger set name and just set the name directly But I wanted to show you that you can go up to the super and call it directly if you wanted to The feline class and if that seems super super confusing, it's because it's meant to be I'm trying to show you that super Is a separate object from the tiger even though they're merged into one class It is a separate entity that python understands and tracks So the difference between saying super set name and just saying set name is very subtle Set name is setting the name in the tiger class where super is saying hey Go up to the feline class and call this function in feline Because remember when we create a class the first thing that gets created is our parent or the super Man that is super super confusing. So this is one of the subtle complexities We're going to have here. So the major takeaway is we have horribly designed tiger. We have Basically created our own constructor And I want to plop a note right here Overwrite the constructor is a bad idea because we now have this chicken or the egg problem Where self dot name does not exist. That's not even defined Let's go ahead and demonstrate all of this just to show that it will explode So I'm gonna say tiger And I want to plop a note right here Is a feline but with a different constructor Different constructor So now there's no name. We've deviated from our class design here. Let's go ahead and print this out Save run and creating a tiger notice how Main tiger object is created. It hasn't exploded Now go back up to stock And remember we're going to print out self dot name But we've never set the name Oh bad things are going to happen Let's watch this fail and then understand why it's failed Uh-oh attribute error tiger object has no attribute name Remember we are overwriting this constructor. So we have to initialize that And it's simply because we didn't do it like this If we had it here it'd be initialized right in the feline and it's initialized for us whenever the feline is constructed But because we didn't do that We're relying on the feline constructor and in the tiger we defined our own constructor. It never gets called up here in feline That is super confusing. So the major takeaway here is unless you have no other choice Do not override the constructor in a child class always rely on the parent constructor Otherwise you have to make sure that you are calling Either the constructor or you're initializing the variables in here Oh, that's why this is called look before you leap because we want to make sure that we actually can do something We want to make sure the name is in the parent. That is the look before you leap principle Uh, so this is what we're doing right here is we are Making sure it exists and look before you leap. There's a lot more to it. But that's the basis Now let's go ahead and run this Suddenly no name Stocking where is it getting no name from? It's getting it from right here. So we're saying tiger create And then we're saying go to the feline and call that constructor, but we don't have a name So we're just going to say no name We could put anything we want there and it would work just fine Let's go ahead and rename this now. So I'm going to say t dot a rename And let's go ahead and call this tony And let's just say t meow just to prove that we are inheriting the attributes of the feline class in T dot stock. Let's call that again I'm going to bring this way up so we can see this in action clear that out and run Okay, so What is happening here? Creating a feline creating a tiger The tiger is created No name is stalking. We've renamed it to tony Tony is now meowing and stalking If we go back into constructor lands And comment this out We're going to have a bad time. I want to really drill that into you The reason why this is crashing is because we are bypassing this constructor and name is never defined Let's watch it fail again Sure enough tiger object has no attribute name Okay, so major takeaways from this video is that inheritance is extremely cool extremely powerful, but has Some hidden issues you really need to pay attention to Do not override the constructor unless you absolutely know what you're doing And you want to make sure you take ownership of that process if you do Easiest ways to call super in it and then call the constructor of the parent class But there are other little gotchas as well And you need to make sure you look before you leap or you do some sort of error checking or you're going to have Some kind of crazy issue I hope you enjoyed this video You can find the source code out on github.com If you need additional help myself and thousands of other developers are hanging out in the void realms facebook group This is a large group with lots of developers and we talk about everything technology related Not just the technology that you just watched and if you want official training I do develop courses out on udemy.com. This is official classroom style training If you go out there and the course you're looking for is just simply not there drop me a note I'm either working on it or I will actually develop it I will put a link down below for all three of those and as always Help me help you smash that like and subscribe button The more popular these videos become the more i'll create and publish out on youtube. Thank you for watching