TypeScript Employee Hierarchy - Abstract Classes and Inheritance
TypeScript Classes and OOP : Exercise-15 with Solution
Write a TypeScript program that defines an abstract class Employee with properties such as name, employeeID, and an abstract method calculateSalary(). Create derived classes (e.g., FullTimeEmployee, PartTimeEmployee) that extend Employee and provide salary calculation logic based on employment type.
Sample Solution:
TypeScript Code:
// Abstract class Employee
abstract class Employee {
constructor(public name: string, public employeeId: number) {}
// Abstract method for calculating salary
abstract calculateSalary(): number;
}
// Derived class FullTimeEmployee
class FullTimeEmployee extends Employee {
constructor(name: string, employeeId: number, private monthlySalary: number) {
super(name, employeeId);
}
// Implement the calculateSalary method for FullTimeEmployee
calculateSalary(): number {
return this.monthlySalary;
}
}
// Derived class PartTimeEmployee
class PartTimeEmployee extends Employee {
constructor(name: string, employeeId: number, private hourlyRate: number, private hoursWorked: number) {
super(name, employeeId);
}
// Implement the calculateSalary method for PartTimeEmployee
calculateSalary(): number {
return this.hourlyRate * this.hoursWorked;
}
}
// Create instances of FullTimeEmployee and PartTimeEmployee
const fullTimeEmployee = new FullTimeEmployee("Veerle Brock", 105, 6000);
const partTimeEmployee = new PartTimeEmployee("Alaya Neha", 104, 15, 40);
// Calculate and print salaries
console.log(`Full-Time Employee ${fullTimeEmployee.name}'s Salary: $${fullTimeEmployee.calculateSalary()}`);
console.log(`Part-Time Employee ${partTimeEmployee.name}'s Salary: $${partTimeEmployee.calculateSalary()}`);
Explanations:
In the exercise above -
- First, define an abstract class "Employee" with properties 'name' and 'employeeID', and an abstract method "calculateSalary()".
- Create two derived classes, "FullTimeEmployee" and "PartTimeEmployee", that extend "Employee". Each derived class provides its own implementation of the "calculateSalary()" method based on the employment type.
- Create instances of FullTimeEmployee and PartTimeEmployee, passing in relevant details like name, employeeID, and salary or hourly rate.
- Finally, calculate and print salaries for both types of employee.
Output:
"Full-Time Employee Veerle Brock's Salary: $6000" "Part-Time Employee Alaya Neha's Salary: $600"
TypeScript Editor:
See the Pen TypeScript by w3resource (@w3resource) on CodePen.
Previous: TypeScript Animal Sounds - Abstract Classes and Inheritance.
Next: TypeScript Geometric Shapes - Abstract Classes and Inheritance.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/typescript-exercises/typescript-class-and-oop-exercise-15.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics