 Hey everybody, this is Brian and welcome to the 33rd Lamp Tutorial. Today we're going to talk about inheritance. Now, what is inheritance? Well, it's not when somebody dives and leaves you a lot of money, although I'm sure some of you out there would probably want that, but anyways, inheritance is a way of making a class inherit the properties of another class. Think about that. Why would you want to do something like that? Well, let's say we have class Mammal, Mammal, sorry, and we have a variable called HasBackbone equals true. All mammals have a backbone. Oops, we've got our visibility modifier here. So there is our Mammal class. Now, that's good and dandy, but what if we want to make a cat class? Well, cat's a Mammal. We could just very easily say HasBackbone equals true and add it in there. But what if we want to make a dog class? Same thing. Now, you could just copy and paste this in here. They're all mammals. They all have backbones. But you say, oops, you know what? Mammals also have, you guessed it, HasHair. So now you have to go back to every class that you wanted to be a Mammal and add in the HasHair variable. And then somebody comes in and says, well, mammals also have this. They also have that, blah, blah, blah. Then you got to do it for each one. Well, there's an easier way of doing this. So let's just delete this out here. And it's called the extends keyword. Now, when we say cat extends Mammal, what do we mean? It means that cat actually inherits all of the goodness that the Mammal class has. Notice how dog does not have the extends Mammal. So let's actually save our work here. Let's open this up. And let's say echo. I've got to create instance first. My cat equal new, echo my cat. Now, notice how cat has backbone and HasHair. And we can even print those out. Even though the cat class has absolutely nothing in it, it's because we've inherited parts of the Mammal class. Now let's try that with dog. Let's say echo. Can't echo it. We haven't made it yet. My dog equals new dog. Let's try echo my dog. Notice how nothing happens because the dog class doesn't have anything in it. The intelligence knows, hey, there's nothing there. Let's extend the dog class. I should say extend Mammal using dog. Now suddenly, magically, HasBagbone hasHair. Now, why would you want to do this? Think about it. Dogs and cats are both mammals. They're both different types of animals. For example, let's add a function. Speak. Well, as you might have guessed, when you say to your cat to speak, it'll meow. When you say to your dog to speak, it'll bark. So let's add these in here. Save or work. And let's actually say my cat. My cat speak says meow. So let's say my dog speak. Wow. Hope if I could spell. Yeah. We kind of messed that up. Let's say dogs don't meow. They bark. Save or work? Misspelled bark. Dogs do bark, but that's a different topic. We're not going to cover in these tutorials. Okay. So as I was saying, you can extend the class, inherit the properties, basically. Cat extends Mammal. Dog extends Mammal. So both of them are mammals. They have all of the properties of a mammal. But they can do different things like cats can meow, dogs can bark or bark if you misspell things. Very simple, very easy concept. But it's also very powerful. Think of it. What could you do? Well, now you could take the cat because I'm a cat person. And you could make a class called lion. And it extends cat. And then you can do other things such as add a function and lions don't really meow. They roar. And we'll just add a... So now we can actually make our lion here. We'll say my lion. Make a new instance of the lion class. Notice how lion now has all the properties of Mammal. It has backbone and hair. It also has the speak function, which we added to the cat class. But it also has roar. So as you can see, you can do multiple levels of inheritance, called an inheritance chain. So now a lion is both a cat and a mammal. And also a lion. Kind of prove that. We can tell the lion to speak. Let's actually just get this out of here. So that we don't confuse it. And we can also make the lion roar. If we could spell correctly. Boy, I've got to do something about this keyboard. Alright, so now we've got meow and roar. They're both coming from the lion class. But when you look in the lion class, you can see the only thing we have in here is roar. That's because it's inheriting from both the cat class and the mammal class. Now you really begin to see the real power of object-oriented programming. And you can do some very powerful things. I wanted to cover inheritance first. Before we cover anything else. Because you're going to really need to understand that concept. That a class is a blueprint. An object is an instance of that blueprint. And you can actually inherit things. For example, the mammal cat lion example that we just wept out. That's all for this tutorial. Thank you for watching.