Java: Abstract Employee class with Manager and Programmer subclasses
Write a Java program to create an abstract class Employee with abstract methods calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that extend the Employee class and implement the respective methods to calculate salary and display information for each role.
Sample Solution:
Java Code:
Output:
Manager Name: Corona Cadogan Base Salary: $6000.0 Bonus: $1000.0 Total Salary: $7000.0 --------------------- Programmer Name: Antal Nuka Base Salary: $5000.0 Overtime Hours: 20 Hourly Rate: $25.0 Total Salary: $5500.0
Explanation:
In the above exercise -
- The abstract class "Employee" has two abstract methods: calculateSalary() and displayInfo(). The subclasses Manager and Programmer extend the Employee class and provide their own implementations of abstract methods.
- The "Manager" class calculates the total salary by adding the bonus to the base salary, while the "Programmer" class calculates the total salary based on the number of overtime hours and the hourly rate.
- In the main method, we create instances of Manager and Programmer, and then call the displayInfo() method on each object to display the information specific to each role.
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Java program where the "Manager" subclass includes a method to assign tasks to employees.
- Write a Java program where the "Programmer" subclass adds a method to track completed coding projects.
- Write a Java program where the "Employee" class tracks working hours and calculates overtime pay.
- Write a Java program where the "Manager" subclass allows employees to request salary increments.
Go to:
Java Code Editor:
Contribute your code and comments through Disqus.
PREV : Abstract Animal Class with Lion, Tiger, and Deer Subclasses.
NEXT : Abstract Shape3D Class with Sphere and Cube Subclasses.
What is the difficulty level of this exercise?