Abstract Employee Class Example
Abstract Employee Class Example
The 'Salary' class extends the 'Employee' class by inheriting its properties and methods, while adding a new attribute 'salary' and overriding the 'mailCheck' and 'computePay' methods. This class specifically implements the abstract functionality of computing pay and handling salary-specific mail checks, ensuring that salary-related logic is centralized within the subclass rather than in the abstract class .
Method overriding in the 'Salary' class involves redefining the 'mailCheck' method from the 'Employee' class to provide additional behavior specific to salaried employees. The overridden method in 'Salary' provides a specific implementation that respects polymorphism, where the method called depends on the object's actual class type, allowing the 'Salary' class to print out salary information while retaining the same method signature .
Method overriding, as demonstrated in this program, involves a subclass providing a specific implementation for a method already defined in its superclass, such as 'mailCheck' and 'computePay' in the 'Salary' class overriding those in 'Employee'. Method overloading, on the other hand, would involve creating multiple methods with the same name within a class but differing in parameters, not evident in this specific code example, aiming to achieve better method functionality while retaining a single conceptual action .
The 'Employee' class cannot be instantiated directly because it is declared as an abstract class. Abstract classes are intended to serve as a blueprint for other classes. They may contain incomplete implementations that subclasses are required to complete or provide, hence preventing them from being instantiated on their own .
The 'Employee' class in this Java program serves as an abstract base class, encapsulating common properties and methods relevant to employees, such as name, address, and number, alongside methods like 'mailCheck', 'computePay', and an overridden 'toString'. Encapsulation is demonstrated here through the use of private fields and public methods to modify these fields, allowing controlled access to the properties and behaviors encapsulated within the class .
The 'computePay' method in the 'Employee' class serves as a placeholder returning a default value, indicating it is designed to be overridden by subclasses. In the 'Salary' class, this method is overridden to provide specific functionality that calculates weekly payments based on the salary attribute, demonstrating polymorphism and extending the basic behavior described in the abstract class .
Polymorphism is exhibited in the main method of 'AbstractDemo' through the use of an 'Employee' reference pointing to a 'Salary' object. This allows calling methods that are overridden in 'Salary', like 'mailCheck', based on the actual object type ('Salary'), not the reference ('Employee'). This demonstrates dynamic method dispatch, as the correct method implementation is chosen at runtime, allowing for flexible and reusable code .
To handle a bonus payment system in the 'Salary' class, introduce a new attribute for 'bonus' and modify the 'computePay' method to include this bonus in the weekly pay calculation. The class constructor should be updated to accept a bonus amount, and methods to set and get the bonus should be added. The new computePay logic might be: 'return (salary + bonus) / 52;' This ensures the bonus is evenly distributed across the year, altering the salary computation system while maintaining encapsulation and inheritance principles .
The design of this Java program is influenced by abstract classes, such as 'Employee', which cannot be instantiated on their own but rather provide a template for other classes like 'Salary'. Abstract classes allow for defining a base structure and methods that must or can be implemented, promoting code reuse and organization within a hierarchy. Instantiable classes like 'Salary' extend abstract classes, implementing specific functionality such as salary calculation, which fulfill the conceptual contract established by the abstract class .
The program demonstrates data hiding by declaring the attributes of the 'Employee' class—name, address, and number—as private, thus restricting direct access from outside the class. Access to these fields is instead provided through public getter and setter methods. This encapsulation is crucial for maintaining control over the data, ensuring that modifications can occur in a controlled manner and protecting the integrity of the data by preventing unintended interference or misuse .