 Hey everybody, this is Brian. This is video eight in our Java tutorial today. We're going to talk about inheritance No, no one's gonna be getting rich. So what is inheritance? Well, we're gonna make a new class Let me move this down so you can actually see it and we're gonna call in an animal So there's our animal class. You see there's nothing in it And we're just going to for the sake of argument say It has a public String name So the animal class has a string variable in it called name And let's make another class here and we'll call it cat Simply because I was thinking of what mess my cat is making in the other room and we will give the cat Public boolean Whoops, we'll say claws Equal true cat has claws, which is why I'm wondering what sort of mischief the cat is getting into right now Save your work Save it again Go back to your start class this handy little guy. We've been working with this whole time Notice how there's nothing in it. We've got our public static void main in our start class Well, we're going to make an instance of this animal class Oops call us M animal Well new animal and we're going to make an instance of the cat Oops, you see how we forgot our Our brackets here. That's called a constructor Similar to a method where you have an arguments list when you have no arguments. You just have brackets Now you see when we take our animal We do animal dot we have some things in here. Well, where did all these come from? We have a name which We added there's our name But we have all this other stuff equals class Hashcode notify way to string. What is all this? We didn't put this in here. Well, you can see over on the right It says object. What does that mean? Well inheritance is a very simple topic meaning Everything inherits from a cosmic base class called an object everything in Java is an object These are all part of the object class or the object blueprint. We see name came from animal Name is part of our animal class now Every class is an object So you are automatically inheriting all of these properties Now let's try cat See same thing. There's all the inherited from the object cat has claws Now we can make our own inheritance chain and to do that go into our cat class and we're going to say extends animal Once again, that's a case sensitive So it will be Access modifier type in this case class the name cat extends meaning we are extending and Then the class that we are extending which is animal. So the cat class is going to extend the animal class What does extend mean? Means you're going to inherit that class in all its glory So now when you type cat dot you see the cat has claws from the cat class. It also has name for the animal class It also has the objects inherited items So that's inheritance in a nutshell Now some languages have what's called multiple inheritance meaning you can extend an Animal and another class another another Java does not do that the designers of Java thought it would be ridiculous for something to You know inherit the properties of an airplane and a refrigerator at the same time even though there are refrigerated airplanes We'll cover something called interfaces in another tutorial, which is a handy way of Bypassing that limitation you can make an interface saying it's a refrigerator and an airplane at the same time But for inheritance, you can only inherit one class Anytime you inherit that class you inherit the properties of that class For example in animal. We're going to say Public void breathe Because animals need to breathe right say system out that print line And let's just say I'm breathing I Have asthma so I actually enjoy breathing all right now when we go to cat type dot You see that we have the breathe method When we run this Save our work See it says breathing So we have a breathing cat as opposed to a non-breathing cat, which is usually an emergency in this household Now notice how cat does not have breathing because once again, it's extending or inheriting the animal class Now eclipse is a pretty intelligent program It'll it'll tell you where you're pulling this from so it'll say name you're getting that from animal breathe You're getting that from animal equals you're getting that from object Claws you're getting that from cat now one more thing before I in this tutorial I kind of wanted to go over here and touch on the packages. See how we have this default package Well, here are our classes inside the package start cat animal Listen out the medical order You can expand out the animal and see that it's the class animal has a variable called name and a method called breathe And same thing with cat There's start so eclipse gives you a lot of information now. What is a package? This is the default package. What does that mean? Well all classes by default are listed in the ironically default package So if we say new package and we'll call this dogs There's our dogs package with nothing in it right-click go new class And we'll call it my old dogs name was Molly There's Molly now you notice how it says at the top package dogs That means Molly belongs to the dog package We can also go ahead and create a new class in the default package called Molly There's our Molly class in the default. Notice how we have two classes Now what happens if we go to make another cat class? Notice how it won't let us It says type already exists Packages are a way of separating your code So let's just for the sake of argument say you make a class called Molly and Your friend makes a class called Molly if they're in the same package you're gonna have a naming conflict and it won't compile So you make separate packages So you can have multiple dogs named Molly or whatever you want to name your objects We'll get into that a little more depth in future tutorials, but Almost out of time. So thank you for watching. I hope you found this video educational and entertaining