In this lecture, we'll implement a small class hierarchy in C#. Here's the class diagram for the classes that we're going to implement. We have family member as the root of the class hierarchy, and the family member's pretty basic, there is a height and weight, and a method that shows that the family member is having fun. We have two child classes of that parent class: Gamer and Geek. And gamer will in fact have a different way of having fun while geek will not. You should go do an in-video quiz, and the answers are base and derive class, and super class and subclass. I will tend to use the terminology parent class and child class, but certainly those other terms are also valid to describe an inheritance relationship. Okay, let's go to MonoDevelop and see how we actually implement this class diagram in C#. We'll start by looking at the parent class in this hierarchy. So, family member. And the family member class has two fields, for height and width, and it has a constructor. So we can create a new family member object, and we pass in the height and weight into the constructor, and then the fields get set to the parameters. We have properties that can get and set both height and weight, and we have a method called HaveFun. This method outputs, I'm writing code because that's how all family members should have fun, and so that is the parent class for the hierarchy. The Gamer class is a child class of family member, and the way that we show that in C# is after the class name, we put colon, and we put the name of the parent class. So if we don't have that, of course, this is just a free standing class, if you will. But if we have this, it means that gamer is a child class of family member. And we've actually seen this a lot because when we create a script in Unity, whatever we've named the script we get colon mono behavior, because every script we create is by default a child class of mono behavior, and that's how we get the start method and the update method and all those methods. This Gamer class has a constructor, that also gets passed in height and weight, but we don't want to include code here in the body of the constructor to set the height and weight, because we know the family member class already has that code. So instead of duplicating this code which we don't need to do, that's one of the benefits of inheritance, instead here in our gamer constructor we will say, call my parent constructor. And so you should go do an in-metric quiz about the base keyword. So base just gives us a reference to our parent, just like this gives us a reference to ourself. So instead of duplicating the code here, we're going to take advantage of the fact that we have a parent class that already does work in the constructor, and we'll call that parent class constructor. We also have this method called HaveFun. Let's go back to a family member and I didn't talk about it while we were here, but this method includes a keyword that says virtual. And you should go do an in-video quiz about virtual. The answer is that we mark a method virtual, so that child classes can override it. In other words, ride over the functionality of this instead of outputting to the console, I'm writing code, a gamer will say, "I'm playing a game!" And to indicate that we are overriding a method in the parent class, we include this key word that says override. So you should go do another in-video quiz about overriding methods. So the answer is that we use the same method signature. So void, havefun and in family member void, havefun. So we haven't changed the method signature at all. We're just saying if you call the havefun method on a gamer object, here's what it should do. Even though the gamer class is a child class of family member, we're going to change the behavior for this method. And remember that's one of the benefits of inheritance, is not only can we share stuff, like in the parent class we can share the fields and the properties and so on, but we can also have our own customized behavior in a child class. A Geek on the other hand, is also a family member, and also calls the parent class constructor in its constructor, but that's all it does. I will say that I know from personal experience that you can be both a gamer and a geek, but we're going to assume that you are one or the other here. So that's actually the class hierarchy. So let's look at our main method in our program file. You'll see here that I'm creating three family member objects. This first one looks like you're used to, right? We've got a family member variable that's going to hold a family member object. The second one might be confusing to you because we say it's a family member, but we're creating a gamer object. But remember, a gamer is a family member. There's that is a relationship that says a child class is a instance of a parent class, even though that's poor English. So we can put a gamer object into a family member variable, because a gamer is a family member. The same with geek, we can create a geek object and put it into a family member variable because a geek is a family member. And now we tell everybody to have fun. So we tell each of these variables to have fun, and the order is family member, gamer and geek. So when I control F5, we see that the family member said, I'm writing code because that's what's in the virtual havefun method in the family member class. The gamer says, I'm playing a game when we call the havefun method because the gamer class overrode the havefun method, and the geek says, I'm writing code because the geek class didn't override the havefun method, so it just inherits the havefun method. So that in a nutshell is how to implement that small class hierarchy. And we've seen how inheritance and overriding and so on worked for the havefun method. Of course, the gamer class and the geek class also inherit the height and width fields, and the height and width properties. And so, we could access those properties for any of these objects because they all inherit them. To recap, in this lecture, we learned how to implement inheritance in C#. And we also learned how to override a method from the parent class in a child.