00:00 You’ve now built a basic program that can calculate the payrolls of various types of employees in a company. I’m going to put the UML diagram on the screen, and I want you to look at the four classes that we’ve created.
Are there any that we didn’t instantiate directly? Yes—that base
Employee class at the very top. It’s only used to define
.id attributes, which get inherited by every other type of employee in our program.
and this time we got an error. It’s saying that the generic
Employee object is missing the
.calculate_payroll() method that the
PayrollSystem needs to calculate its payroll. That’s because that method is normally implemented by the specialized versions of
This isn’t actually a problem, though. The only reason our program broke is because we used it in a way it was not intended to be used. The
Employee class exists just to be inherited from—not to be instantiated itself. That means it’s what’s called an abstract class.
Now I’m going to make the
Employee class inherit from
ABC, and I’ll create a new method called
.calculate_payroll() so that this class conforms to the
IPayrollCalculator interface. I’ll write
pass in here to make it blank.
One, they cannot instantiate the
Employee class directly, and two, the
Employee class can only be inherited from. When a class inherits from an abstract class, that class should either provide its own implementation for any of the methods in the parent marked as abstract, or it should become an abstract class in and of itself, leaving implementations of the parent’s abstract methods to its child classes.
In other words, it’s saying, “That implementation is not my problem, so I’m going to make my children implement it.” OOP really does model the real world. Anyway, all three specialized employee classes provide their own implementations of
.calculate_payroll(), so all is good.
Instead, it lets its children classes do that. If we were to simply remove the instantiation of the general
Employee in our main
program file and take it out to the list, everything would work perfectly fine. However, to more accurately follow the UML diagram from earlier, I’m going to remove everything about the
Employee class that makes it abstract, including the
Become a Member to join the conversation.