3.2.4 How to Utilize the Inheritance Hierarchy

Inheritance is the idea that one thing can _have_ the members of another class. Additionally, classes can go from more generic to more specific many times; this is called the inheritance hierarchy. This is much like taxonomy in biology where a Red Fox has a Species of Vulpes vulpes, Genus of Vulpus, a Family of Canidae, an Order of Carnivora, a Class of Mammalia, etc. each getting more generic up the chain as it goes. Inheritance in C# is the same idea. A Manager class inherits from Boss class inherits from an Employee class. Because objects are composed of all these classes combined they have access to all inherited methods/fields.

For this example we will be having a class (Boss) inherit from a parent class (Employee). What this means is that any Boss type object will now also have all of the members of the Employee class available to it as well as any other specialized members defined in the Boss class.


We use inheritance to reduce redundancy in our code. As it pertains to the Boss/Employee relationship, an Employee object would have a number of members that could also apply to a Boss object such as moneyBalance, TimeSheet, and Eat().

But say we wanted to create a Boss class with all of the things the Employee class has plus some other stuff? It would be redundant and unweildy to define a whole other class with the same exact members. If you needed to update a method you would need to do it in two places now.

So what about putting those methods/fields in the Employee class? As much as I'm sure regular Employees would love to give themselves raises or fire everyone they shouldn't be able to. Those tasks should only be given to a Boss type of object. So what's the solution? We will have Boss inherit from Employee to gain all of the functionality of the Employee class while still being able to have members of their own.

The inheritance hierarchy can continue and multiple classes can inherit from a parent class.

Now the BoothManager has everything the Boss and Employee classes have as well as a field that is only applicable to them and two fields only a BoothManager type can use. In much the same way, the StoreManager has everything the Boss and Employee classes have as well as two methods that only StoreManager types can use.