[Go to site: main page, start]

0% found this document useful (0 votes)
11 views2 pages

Abstract Employee Class Example

The document describes an abstract Employee class with fields to store an employee's name, address, and number. It contains methods like computePay and mailCheck. The Salary class inherits from Employee and adds a salary field. It overrides mailCheck and computePay to handle salary specific logic. The main method demonstrates creating Salary objects and calling methods on both Salary and Employee references.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Abstract Employee Class Example

The document describes an abstract Employee class with fields to store an employee's name, address, and number. It contains methods like computePay and mailCheck. The Salary class inherits from Employee and adds a salary field. It overrides mailCheck and computePay to handle salary specific logic. The main method demonstrates creating Salary objects and calling methods on both Salary and Employee references.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/* File name : Employee.

java */
public abstract class Employee {
private String name;
private String address;
private int number;

public Employee(String name, String address, int number) {


[Link]("Constructing an Employee");
[Link] = name;
[Link] = address;
[Link] = number;
}

public double computePay() {


[Link]("Inside Employee computePay");
return 0.0;
}

public void mailCheck() {


[Link]("Mailing a check to " + [Link] + " " + [Link]);
}

public String toString() {


return name + " " + address + " " + number;
}

public String getName() {


return name;
}

public String getAddress() {


return address;
}

public void setAddress(String newAddress) {


address = newAddress;
}

public int getNumber() {


return number;
}
}

Inheriting the Abstract Class


We can inherit the properties of Employee class just like concrete class in the
following way −

Example
/* File name : [Link] */
public class Salary extends Employee {
private double salary; // Annual salary

public Salary(String name, String address, int number, double salary) {


super(name, address, number);
setSalary(salary);
}
public void mailCheck() {
[Link]("Within mailCheck of Salary class ");
[Link]("Mailing check to " + getName() + " with salary " +
salary);
}

public double getSalary() {


return salary;
}

public void setSalary(double newSalary) {


if(newSalary >= 0.0) {
salary = newSalary;
}
}

public double computePay() {


[Link]("Computing salary pay for " + getName());
return salary/52;
}
}

Here, you cannot instantiate the Employee class, but you can instantiate the Salary
Class, and using this instance you can access all the three fields and seven
methods of Employee class as shown below.

/* File name : [Link] */


public class AbstractDemo {

public static void main(String [] args) {


Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);
Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
[Link]("Call mailCheck using Salary reference --");
[Link]();
[Link]("\n Call mailCheck using Employee reference--");
[Link]();
}

Common questions

Powered by AI

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 .

You might also like