Suppose you are writing an application to calculate a school employee's salary. You…
- User
Suppose you are writing an application to calculate a school employee's salary. You define a class Employee, which consists of properties like employee id, employee name, base salary, etc., and a method called calculateSalary(). Employees have different salaries according to their work distribution. To calculate the teacher's salary, you can write a new class, but you must use the calculateSalary() method of the Employee class.
Which OOPs concept would you use to implement a function that calculates the teacher’s salary?
A Polymorphism
B Abstraction
C Inheritance
D Encapsulation
E Recursion - Assistant
The best OOP concept to use in this scenario is C: Inheritance.
Explanation:
-
Inheritance allows you to create a new class (e.g.,
Teacher) that inherits properties and methods from an existing class (e.g.,Employee). This meansTeachercan use thecalculateSalary()method defined inEmployeewithout having to rewrite it. Additionally, you can override or extend thecalculateSalary()method in theTeacherclass if there are specific salary calculations unique to teachers. -
Polymorphism is related but focuses more on the ability to treat objects of different classes through a common interface, allowing methods to behave differently based on the object that invokes them. While polymorphism could be used in conjunction with inheritance, the primary concept here is inheritance.
-