 Okay, welcome to intro to Java with an emphasis on AP computer science a with Tokyo ed tech That is me our topic today is very important. It's called inheritance. So what we're gonna learn about is first What is inheritance? We're talking about the idea of a superclass versus a subclass We're gonna learn about the extends keyword and the super keyword very important And we'll learn a little bit more about method overriding method overriding is very similar to method overloading and You'll see a little bit what that looks like So the first thing is what is inheritance and here's a little chart to kind of kind of display to show you what that kind of looks like so I'm not sure why you can see the if you can see the things there, but so Basically what we're trying to say with inheritance is we're trying to organize our classes organize our objects into a hierarchy So you can see here that person as A name and an age We have our mutator methods are setters. We have our accessor methods are getters And in this case the person class is gonna be what's called our superclass And then below the person class we have two subclasses a student and a teacher So what this says is that a student is a person a Teacher is a person So as a person a student has a name and an age An associated setters and getters a teacher has a name and age and associated centers and getters But a student also has an extra attributes in this case grade So grade level and the centers and getters for that and the teacher has an extra attribute in this case department It's a string. We have a setter and a getter for that. So this allows us to program our objects into a hierarchy So let's go ahead and take a look at how to do that. So first We have our first person class. This is the same person class we've used in previous videos So we've got two private attributes name and age. We got our setters. We got our getters this case We have just a default constructor There's another structure there and then we have one non-static method called say greeting. So let's go ahead and program that so let's say Person say Bob equals new person And we'll say Bob dot set name Let's see what's Obviously Bob said that name's gonna be Bob And say Bob dot set age Let's give Bob an age here. We'll call him say he's 21 All right, so Nothing new here is same stuff. We've done all along and So I can come so say I can compile this And I can go ahead and compile this Now what I can also do is I'm gonna go ahead and just do this and say person sue equals new person I'll say sue dot set name Sue I could have made a good structure for this, but we'll just do it this way If you look at my book, it's done with the non-default constructor set age And we'll say how soon she's gonna be 17 and we'll say person Jason was new person And we'll say Jason that's that's name just Jason And Jason dot sets age All right, and let's go ahead and compile it just to make sure everything's working Okay, now we didn't see anything when we executed it because of course We didn't print anything. Well, we could do we could just go we could do a good Bob dot. What was it? Say great. Let's go ahead and do that one. So say greeting Go ahead and copy that paste it Say sue Yes, let's just test it make sure it's working Okay, so we've got hi. My name is Bob. I knew sue. Hi. My name is Jason. So up to this point There's nothing new that we haven't done before Okay, so what we want to do though is we want to Make a new class called student So it's gonna look like this student sue equals new student and to do that I'm gonna go up here and do Extends I'm gonna compile that I'm gonna go back here and see what I want to compile it and run it and there should be no change Okay, so notice This class has no methods has no setters no getters no attributes But everything's working because let me get back to a little chart there We have said that the student Extends the person class so because of that it inherits all of the attributes all of the methods of The parent class of the superclass. Sometimes you hear called parent class pretty cool. So let's go back Do the same thing with the teacher class? say extends Person so we can go down here to this. We say teacher So now what's really cool about this is now I can go to my student class and I can add attributes just for that class. I'd say private int grade equals zero And then we do our public setters and getters public void set grade into grade And we'll say this grade grade We'll do our getter public int return type get grade Return and this grade. So let's go ahead and compile that and Test it. So we can say system.system out println grade Sue get grade compile it execute it So I forgot to set the grade so we'll say Sue is a Senior solutions So you see now Sue now has three attributes She has two attributes from her parent class from the parent class and then one attribute added Just for the student class. I can now do the same thing with the teacher class. I could say you know private string department and then public void set department string department It's very repetitive, doesn't it? This dot department equals department Actually, there are IDs that will do this for you. Just push a button and it takes care of it String get department, but it's probably good practice as a beginner to type all this stuff out And return this Let's go ahead and give Jason a department Say Jason dot set department meant Art lazy, so I'm gonna go ahead and copy this and you can see here so now Bob we have a Bob object, which is a Person we have a sue object, which is a student and we have a Jason object, which is a teacher So what's cool about this is let's say later in our program. We needed to add something So if we needed if we wanted to add an attribute What's something so let's say like double height for whatever reason we need this health information 0.0 We would only have to add it here in the person class and then the student class and teacher class would Automatically get that you know get that attribute added as well So this is one of the useful features of oops one of the useful features of object oriented program And so that you know the way you organize your code can be very very efficient Okay, so I was gonna get the method overriding but let me do one other thing really quickly Because there is another thing you need to know So what I want to do is I'm gonna add a new constructor instead of the default constructor So yeah, I didn't run yeah, I'll go like this. So this is gonna be person Instructor, so it's gonna be person string name into age And it's gonna be this dot name equals name And this dot age equals age So I'm gonna go to my student class So what I need to do in here is I'm gonna do the same thing. I'm gonna do students string name Int age and there's other ways to go about these things What I have to do here Oh, and I'm gonna put Int grade What I have to do here is the super keyword So let me explain how that works. Well, actually, let's go ahead and just test the code first So what I'm gonna do is person. It's gonna be bob And he's 21 we said So I'm gonna get rid of that So we've created a new constructor Same thing here was sue So she's 17 and she's in grade 12 So we're gonna get rid of all these things Let's go to the pilot see if it works Here kind of find simple spell it string wrong Uh person You guys probably noticed that I did not Okay, let's try it again Let's try to compile it first time and I'm about to avoid it and it's not found. What did I do wrong? Teacher job 11 team Ah, okay. So, okay. We gotta program this one too. Um, so I'm gonna do teacher String name Int age string department. I'm gonna go ahead and do Super name age And this dot department equals department Let's try and compile that So compile that a lot of stuff here to keep keep a track. Oh, I'm gonna put this in correctly now 48 And art I didn't get rid of all that I'll be in So you see everything's working as we wanted it. So let me explain this one more time here. Um, so Here is our bob Uh Object instantiation. So that's a person So we've got string name int age and this is this is what we've been doing all on this is this is from previous unit Now a student We have to call the super class constructor if we don't it calls the default one. So name and age So it comes to here because the parent classes person name and age Same thing with teacher dot java We call the super class constructor Which has name and age that that initializes our name and age values and we also added our this dot department equals depart And then down here everything works exactly as it did before So that is that is the main kind of gist of inheritance Where we have a parent class with some attributes. Let me get back to the chart We have parent class with attributes and methods They are inherited by the subclass or subclass is and the subclasses can have their own attributes that only they have and uh So the way you do that is to create constructors In the subclass constructors you need to first you need to extend Then you need to call the super class. So those are kind of two really important steps. So extends and Super class constructor And then the final thing is method overriding. Uh, this is similar to method overloading So method overloading was like method And then method, you know string And then let's say method, you know, we just saw that with the constructors comma int So bait method, uh overloading is based on the signature Method overriding is a little bit different. So if you look up here in our Class we have say greeting so That is inherited by the student class. It is inherited by the teacher class But what we can do Is we can copy this Pop it into here And then we can add I can copy this Into my teacher class compile all this stuff. Uh, this is fascinating. Okay And this was going to come up somewhere Okay Okay, so this is kind of a weird one. Uh, so You notice here. Hi, my name is name Does not work Because it is inherit it is private And it is inherited from the person class So to do that We need to do this dot get name So we have to use the public Accessor method We do the same thing here in teacher This dot get name Okay, so now I'm going to run this Let's see what happens here. It's based on our department. Just for good measure Okay, so you see here We did say greeting for Bob. Bob is a person. So for Bob, it just says hi. My name is Bob For sue it says hi. My name is sue. I'm in grade 12 For Jason it says hi. My name is Jason. I'm in the art department So this is method overriding Okay, so what happens is There's if there's a method It first looks here Okay, if it finds that method It calls it if not It goes up to the parent class and looks for the method It's there. It calls the parent class method So we say that this method Overrides or the subclass method overrides The parent class method So overrides like takes over So depending on What type of object in this case teacher Student it will call the method for that particular If it exists So it'll go up that chain sort of from the subclass to the superclass If it doesn't find the subclass it looks in the superclass because it inherits it But what we can do is we can override And make a custom one for the subclass This is a pretty powerful thing Okay, so I think that about covers it. I thought it was a lot So you might need to watch that one again because there's a lot of little code nuances And there are a lot of little things to keep track of so we looked at what inheritances We realized what a superclass and subclass are looking at that nice little chart And we looked at the two key words we need extends and super Extends tells java, which is the superclass And then the super keyword calls the superclass constructor And then we looked at the idea of method overriding. So thanks for watching and good luck