Just wanted to list a few key features of abstract classes that might help you grasp the concept.
So what defines an abstract class?
Consider using abstract classes if any of these statements apply to your situation:
Benefits?
Example
Lets say that you are designing an accounting software. You're on the payroll section and you want to be able to allow the user to pay employees. There are different types of employees though: full-time, contract, part-time, etc.
You create the full-time and contract employees. Each have the following properties: id, first_name, last_name and each have the following methods getName() and getMonthlySalary(). The full-time employee class also has the annual_salary property and the getMonthlySalary() method computes the monthly salary by taking the annual_salary property and dividing it by 12. The contract-employee doesn't have an annual_salary property. He has an hourly_rate and hours_worked. The getMonthlySalary() generates the monthly salary by taking the hourly_rate property and multiplying it by the hours_worked property.
You'll notice that id, first_name, last_name, getName() and getMonthlySalary() exist in both classes. You remove that code and place it in an abstract class. Because the getName() method does the exact same procedure in both classes, you can place the statements for that method within the abstract class. For getMonthlySalary(), you'll just declare the method in the abstract class and override them in the subclass placing the individual code needed for the calculation. The abstract class can be called BaseEmployee.
So what defines an abstract class?
- A class with the abstract reserved word in its header.
- Abstract classes are distinguished by the fact that you may not directly construct objects from them using the new operator.
- An abstract class may have zero or more abstract methods.
Consider using abstract classes if any of these statements apply to your situation:
- You want to share code among several closely related classes.
- You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
- You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
Benefits?
- Code management and speed of modification.
- Cleaner code. If you have certain methods that you know will always have the same code: i.e. getFirstName() { return $first_name; }. In an abstract class, you can place the abstract method and it's statements (unlike an interface where you can't place any statements).
Example
Lets say that you are designing an accounting software. You're on the payroll section and you want to be able to allow the user to pay employees. There are different types of employees though: full-time, contract, part-time, etc.
You create the full-time and contract employees. Each have the following properties: id, first_name, last_name and each have the following methods getName() and getMonthlySalary(). The full-time employee class also has the annual_salary property and the getMonthlySalary() method computes the monthly salary by taking the annual_salary property and dividing it by 12. The contract-employee doesn't have an annual_salary property. He has an hourly_rate and hours_worked. The getMonthlySalary() generates the monthly salary by taking the hourly_rate property and multiplying it by the hours_worked property.
You'll notice that id, first_name, last_name, getName() and getMonthlySalary() exist in both classes. You remove that code and place it in an abstract class. Because the getName() method does the exact same procedure in both classes, you can place the statements for that method within the abstract class. For getMonthlySalary(), you'll just declare the method in the abstract class and override them in the subclass placing the individual code needed for the calculation. The abstract class can be called BaseEmployee.
Comments
Post a Comment