Understanding the Usage of Abstract Classes
Abstract classes allow us to further extend inheritance in objects. Suppose we have a system that is tracking employees, and we want to implement a function to calculate the pay of our employees. We have a base class, Employee, which implements the name, employee ID, and age of the employee. We want to create a derived class for SalariedEmployee and HourlyEmployee, which can be extended to have functions to calculate the pay of the employee. One further problem is that the pay of our salaried and hourly employees varies by region. Due to this, we want to allow each region’s programmers to implement their own pay functions while keeping the base structure of the SalariedEmployee and HourlyEmployee classes.
This sort of situation is one where abstract classes might be valuable. An abstract class allows us to define functions that should exist, without an actual implementation of the function itself. Doing this forces programmers using the class to implement the required functions, but doesn’t provide any default functionality. In the case of calculating pay, we want each programmer extending our classes to implement their own pay functions. However, there is some danger in having a default pay function, since it can lead to incorrect numbers if the programmer doesn’t extend the function.
We do this by creating an abstract method within our class. An abstract method is one that must be implemented but doesn’t have a default implementation. We additionally need to define our class as abstract to tell the compiler…