 Welcome to Intro to AP Computer Science for new AP teachers. The topic of the video is inheritance. And this is one of those topics that actually is kind of fairly easy for students to grasp. Once you've gotten to this point, this one's pretty straightforward. So in this video, I'll talk about what is inheritance. The example I'll use is a person. A student is also a person and a teacher is also a person. But they have some differences because students have certain attributes whereas teachers have other attributes. I'll talk about the actual coding of inheritance. So using the extends keyword and the super method. Then we'll talk about method overriding, which is similar to method overloading. Sort of. There's definitely an analogy there. And I think there's a little practice exercise. I just don't have it on the screen. So what is inheritance? Inheritance is the idea that classes can have a hierarchical relationship. Isn't that a nice way of saying almost nothing? So in this relationship, there is a superclass and a subclass. A superclass can have multiple subclasses. And subclasses inherit the attributes and methods from the superclass. I'm going to give you a nice little chart here to illustrate what all of that means. So you'll see here, we've got a superclass called person. And it has two attributes, string name and int age. It's got four, two mutator methods and two accessor methods. Set name, set age, get name, get age. And what we've said and what we're going to say is that a student is a subclass of person because a student is also a person. A teacher is also a person. Although some of your students might disagree with that assertion. So then the student has an attribute that the person doesn't. So a student is in a certain grade. So grade 9, grade 10, grade 11, grade 12, for example. And we have our setter and getter for that attribute. Teacher doesn't have a grade because teachers were out of school. And but we do work for a particular department in many cases. Cases of media. So string department. So we have set department and get department. But because we say that student and teacher are subclasses of the person superclass, the student also has a name. So it also has an age. So it also has setters and getters. Teacher also has a name. Also has an age. Also has the setters and getters. So these attributes and methods are inherited by the subclasses. That's really powerful. It's a really powerful way of organizing because if we want to add something here, it's automatically added down here, which is pretty darn cool. And this comes in handy in a variety of situations. So to do this, excuse me. So to do this, we need to set up this relationship in the code. So we use what's called the extends keyword. So for example, class student extends person. This tells Java that the student class is a subclass of the person class, as we saw back here. So class student extends person. And the same thing, class teacher extends person. Then in the constructor, we also need to call that superclass constructor. So now if we don't do that, it will call the default constructor for us. Then everything's just kind of set up. But if we want to use one of the non-default constructors from the last, hopefully the last video, if you watch that one, from the last unit, then we need to specifically say supers like name, age. We need to actually call that particular constructor. If we don't, the default constructor will be called for us, which is kind of convenient. Yeah. Method overriding. This is very similar to overloading. So if you recall, in the case of overloading, we had the same method, but different arguments. And based on the signature, it would call that particular method. So this is similar, except in this case, the method and signature is the same, but the method is in a different class. So when calling a method, first check the subclass for the method. If it exists, call it. If not, check the superclass and we call that one instead. So let me go back to my little chart here. So if this had like, say something, and the student says, hey, what's up? This says say something, do your homework. And this says say something, the person might just say hi. So if you're a student, it'll check here for that method first. If it doesn't find it, it'll check here. And again, this is explained in the book in detail. So the introductory exercise. So explain the idea of inheritance. And I would draw that little chart on the border, show that slide. And ask the students to come up with their own examples. They can do this in pairs and then just write this on a piece of paper. And so the superclass should have a few attributes. So give that superclass a few attributes and give each subclass at least one unique attribute. So so again, the example I gave is person and student teacher. And you'll actually find this. I found that the students had a lot of trouble figuring out that, you know, what is a what's a superclass and what's a subclass? They kind of, you know, they went ahead of backwards. They say student in person, which when it should be the other way around. It's kind of interesting to see what they come up with. A lot of them will come up with scientific examples, you know, maybe bird and then or what they'll do is they'll say bird and then like parakeet and swallow. And you have to tell them, no, that's parakeet and swallow. Yes, they're birds, but those are more instances of what a bird is. So that that's one of the things where they get confused is, you know, a class versus an instance. And it's interesting. It's interesting mental exercise. And then, you know, you can extend this. And once they've written it down in paper, then go ahead and code it and see if they can get that relationship working, you know, add some test values, do some constructors and then, you know, print out some values just to see if they can get that to work. So yeah, so that is it. That's what we did. We looked at what is inheritance, an example of inheritance, person and student teacher, the extends keyword, the super method and method overriding. And although I didn't put on this slide a little introductory activity, it'll help cement these concepts after you've explained them. So yeah, that's it. Take it easy. Have a good one.