Understanding Inheritance 141 . (Free web hosts) When to Use Inheritance
Understanding Inheritance 141 . When to Use Inheritance The Employee class has a field named salary, which implies that an employee has a salary; however, hourly employees have an hourly rate, which is quite different from a salary. A tempting fix for this situation is to add a Boolean field to the Employee class named isSalary that is true for salary employees and false for hourly employees. The salary field could be used to represent an annual salary when isSalary is true or an hourly rate when isSalary is false. The isSalary field could also be used within the computePay() method to determine which arithmetic to use because computing pay for salaried employees is different than for hourly employees. For example, public float computePay() { if(isSalary) { //Perform arithmetic for salaried employee } else { //Perform arithmetic for hourly employee } } Adding a field such as isSalary and trying to use one class to represent two different types of objects is not a good OOP design. You could make it work, but you are not taking advantage of the benefits of inheritance. If you use a field to determine the type of an object, your end result is a class that looks object-oriented but is really procedural. For example, an Employee object will need to check this added Boolean field just to know what type of object it is, causing the design of the program to not be focused on objects. A bigger problem arises when something needs to be changed. What happens to the isSalary field when a new type of employee needs to be added? Suppose that the company starts hiring contractors who are paid by the day. The Boolean no longer works because it can t be used to distinguish between three types. You could change the field to an int, name it employeeType, and use an enumeration like 0 for salary, 1 for hourly, and 2 for contractor. Again, you could make this work, but you have to make serious modifications to the Employee class. The computePay() method will have to entirely rewritten: public float computePay() { switch(employeeType) { case 0: continued
Note: If you are looking for cheap webhost to host and run your apache application check Vision apache web hosting services