13 - Inheritance
Inheritance is the first of three primary pillars of object oriented programming that we will go over. It enables programmers to re-use classes with slight variations thereby simplifying code. To use the object oriented concept of inheritance, we start with a generalized class which we will call the base class. The base class has variables and methods that all deriving classes should have within them. For example, we can generalize create an animal class as a base class. For this tutorial we will say that every animal has a name, weight, length, and rgba color. We can further create a subdivision also known as a derived class which has more variables and methods associated with it. For instance, we can derive further from our Animal class the classes, Mammal and Fish. Mammal may have variables and methods that do not occur in fish such as hair count, and movementType. Fish may have variables and methods that mammals do not have -- numberOfScales, swim pattern, etc. You can see that the derived class contains additional information that is not included in the base class. Doing this saves on memory and complexity of a program; you do not to fill in variables which are irrelevant to what you are trying to accomplish.
In the example below we will look at a typical fantasy style game enemy breakdown. This example will be very basic and each class will only show a few variables and methods just to prove a point. We first create a base class of enemy which has default variables of name, hp, and basedmg and a default method of displayBanter. Every enemy in the game must have these variables and methods. We additionally would like to create two subtypes of enemies called mage and archer which inherit from the enemy base class. These derived classes add either a spell and cast variable or a ammo and shoot method respectively.