In this lecture, we'll implement a small class hierarchy in a C Sharp console app. 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 is pretty basic. There's a height and a 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. Okay, let's go implement this class hierarchy in C Sharp. We'll start with our family member class, which is the top of the class hierarchy that we designed. And the family member class has two fields for height and weight. It has a constructor that has height and weight parameters and then it assigns each of the fields to the corresponding parameter. And we use the this keyword so that we can distinguish between the height field and the height parameter. We learned that way back when when we learned how to implement constructors. So we do that again here. The class has two properties, it has a height property that we can get and set and a weight property that we can get and set. And it has a HaveFun method, we'll talk about the virtual keyword when the time is right, but we have the HaveFun method with no parameters and a void return type. And this just says, I'm writing code. And that's all there is to the family member class. The Gamer class is a child class of the family member class. And we can tell that because the class name is followed by a colon and then the name of the parent class, in this case family member. And we've actually seen this before, all the scripts that we create in Unity, we have our script name followed by colon and mono behavior. So all our Unity scripts actually inherit from mono behavior and that's what gives us access to the start method and the update method and so on. So we've been using inheritance for a long time, but now we're exploring how it really works. Okay, so the Gamer class inherits from family member. We need to implement a constructor for the Gamer class because remember, constructors have the class name as the name of the constructor. So we need a constructor that's called gamer, we'll pass in the height and weight. But we don't want to copy this code from the body of the family member constructor and paste it here in the body of the Gamer constructor. One of the benefits of inheritance is we can just use the constructor from our parent class. And the way we do that is after we put our typical constructor header, we put a colon and we say base and we pass in height and weight. And what this does, is it calls our parent constructor from this constructor before it even gets to the body of the constructor. So, when we call the Gamer constructor passing in height and weight, it immediately calls the family member constructor with height and weight, which sets those fields appropriately. Every time we build a child class, we're going to want to implement a constructor that calls the constructor for the parent class as the very first thing it does. And that's all we have to do in the Gamer constructor. So a Gamer is going to change the behavior of the HaveFun method. Although we inherit lots of stuff from our parent class, including the height and weight fields and the height and weight properties and the HaveFun method. We actually want this particular child class to have different behavior than the parents HaveFun method. There are two things we do to make that happen. Here in the parent class, we mark the method that we're going to override or that we may override with the virtual keyword. And basically that says children are allowed to override or change the behavior of the HaveFun method. Here in the Gamer class, we indicate that we're overriding that method by putting the override keyword. And we have the exact same return type method, name and list of no parameters in this particular case as the method we're overriding. So what a Gamer object will do when it's HaveFun method is called is, instead of saying, I'm writing code, it will say I'm playing a game. And that's it for the Gamer class. Our Geek class is also a child class of the family member class. So it inherits everything from the family member class. We have the constructor for our Geek that just calls the constructor in the family member class. And that's actually all we do here in our Geek class. Those are the classes in our class hierarchy, let's go take a look at our main method. Here in our mean method, we start by constructing family members. So this first variable called family member, looks just like you've seen before as we create objects. We have the data type, we have the variable name, we call a constructor and the constructor is named family member and we pass in the height and the weight. The thing that's different here, if you look at this line of code, we see that the gamer variable is declared to be of type family member. We could put gamer here, but as we'll discover, especially as we learn about polymorphism in the next lesson, we'll see that it's valuable to actually call this a family member, not a gamer. We create a Gamer object though and store it in the gamer variable. And the reason we can do that is because a gamer is a family member, we have an is a relationship when we use inheritance. So because a gamer is a family member, we can put a gamer object into a variable that we've declared as a family member type. And we do a similar thing here on this line of code where we create a Geek object and then we store that geek object in a geek variable that is also of type family member. Now, we tell everybody to have fun. So we tell the family member to have fun and the gamer to have fun, and the geek to have fun. When we run our code, the first line of output is from the family member. So that's the have fun, I'm writing code. The second line is from the gamer. So the gamer has overridden to HaveFun method. So when the HaveFun method is called on the Gamer object, it calls the HaveFun method for the gamer class, not the family member class. The third line of output is from the Geek object. And because the Geek object didn't override the have fun method from the family member class, it inherits the HaveFun method, just as it stands in the family member class. So it says I'm writing code. Now, I will freely admit from personal experience that you can be both a gamer and a geek but for the purposes of this example, we have those two different behaviors. We don't combine them in any way. So at its most basic level that is demonstrating inheritance, showing how we can inherit methods and we inherited fields and properties as well. We could have overridden properties and so on, or added fields to our child classes, which we'll see in the next lecture. But this example shows that we can both override a method from our parent class or just inherit the method from our parent class. To recap, in this lecture, you learned how to implement a small class hierarchy in a C Sharp console app, and you also learned how to override a method in a child class.