You want to design a system where different types of employees calculate their salary differently. Which approach best uses abstract and concrete classes?
hard📝 Application Q15 of 15
Java - Abstraction
You want to design a system where different types of employees calculate their salary differently. Which approach best uses abstract and concrete classes?
ACreate only concrete classes for each employee type without any abstract class.
BCreate an abstract class Employee with an abstract method calculateSalary(), then create concrete subclasses like Manager and Developer implementing it.
CUse an interface with no methods and concrete classes implementing it.
DCreate a concrete Employee class with a fixed calculateSalary() method used by all employees.
Step-by-Step Solution
Solution:
Step 1: Identify need for shared rules with different implementations
Employee types share concept of salary calculation but differ in details.
Step 2: Use abstract class with abstract method
Abstract class Employee defines calculateSalary() abstractly. Subclasses implement specific logic.
Step 3: Evaluate other options
Create only concrete classes for each employee type without any abstract class. lacks shared abstraction. Use an interface with no methods and concrete classes implementing it. uses interface with no methods, so no contract. Create a concrete Employee class with a fixed calculateSalary() method used by all employees. fixes salary calculation, no variation.
Final Answer:
Create an abstract class Employee with an abstract method calculateSalary(), then create concrete subclasses like Manager and Developer implementing it. -> Option B
Quick Check:
Abstract class sets rules, subclasses do work [OK]
Quick Trick:Abstract class for rules, concrete classes for details [OK]
Common Mistakes:
Not using abstraction for shared behavior
Using concrete class with fixed method only
Interfaces without methods don't enforce contracts
Master "Abstraction" in Java
9 interactive learning modes - each teaches the same concept differently