Inheritance

In most cases, there are programming projects where a user is able to make all aspects of the program themselves and only have to import other classes as necessary. In a few cases though, classes must serve as an extension of an already existing class as it uses the same methods, but in some cases either adds or overrides the parent class's methods for the program to work the best. With that in mind, I'll cover a basic idea for how inheritance works.

First, think of a company worker. This company worker has a name, gets paid a certain amount of money an hour, is either male or female, is of a certain age, and usually holds a certain position. The basic reality is though that many people make the same amount of money an hour, but there are different groups with different wages. Now, we can either create a series of classes to cover each position, or we can make an abstract class to represent a worker within the company, and make child classes that extend this abstract class which will allow for a great deal of flexibility in this case. Here's an example how inheritance can be used to identify company workers.

Parent Class: Worker
Wage: $7.25/hr
Position: Worker
Accepts name, gender, age variables in constructor

Child Class: Salesperson
Wage: Same as parent class
Position: Salesperson
Accepts same variables as Worker class

Child Class: Manager
Wage: $9.00/hr
Position: Manager
Accepts same variables as Worker class

Child Class: TechAssistant
Wage: $7.75/hr
Position: Technical Assistant
Accepts same variables as Worker class

We see that all of the child classes inherit from the worker class, override information as necessary for the position, and can use all of the same methods as the worker class along with having their own. Should a different type of worker be introduced, we can create a class with that information instead of going to great pains to add their information. In addition, for the child class, they do not have to know about the information held in other child classes, which allows for a level of Data Abstraction, which Allen Holub considers to be a very important facet of Object Oriented Design.

Now, in the Java programming language, inheritance takes place when the class is being named using the reserved word "extends" as seen in this example:

public class TechAssistant extends Worker
{
     // Actual code for the class.
}

In C# the concept is similar, but the syntax is slightly different as seen in this example:

public class TechAssistant : Worker
{
     // Actual code for the class.
}

For C++, it uses the same syntax as C#, but with the difference of specifying the public base of the parent class as seen in this example:

public class TechAssistant : public Worker
{
     // Actual code for the class.
}

As a result of this, we can use the methods created in the Worker class and if need be, we can also override these methods. (I will mention though that overriding methods given by an abstract class such as Worker is usually not recommended.)