Commit 53076ec
committed
feat: Add Bonus and Programmer classes to demonstrate inheritance in salary calculation
WHAT the code does:
Defines Bonus with an int bonus = 10000.
Defines Programmer extending Bonus with:
- basicsal = 50000
- totsal as total salary
- salary(): computes totsal = basicsal + bonus and prints it.
Defines InheritBonus with main():
- Creates Programmer object
- Calls salary() to print combined salary (60000).
WHY this matters:
Demonstrates **inheritance** in Java: Programmer inherits the bonus field from Bonus.
Shows how subclass fields (basicsal) and superclass fields (bonus) can be combined for new behavior.
Provides a simple example of code reuse through inheritance in a business domain context (salary calculation).
HOW it works:
Programmer inherits bonus = 10000 from Bonus.
basicsal = 50000 is defined in Programmer.
salary() adds both → totsal = 60000.
Printed output: 60000.
Tips and gotchas:
Fields are package-private (default access). Best practice is to mark them private and provide getters/setters for encapsulation.
Class names should follow Java conventions: Bonus and Programmer are correct, but InheritBonus is more idiomatic as BonusDemo or SalaryTest.
Business logic is overly simplified; real salary systems would include multiple allowances, deductions, and validations.
Instead of directly accessing superclass fields, consider calling methods (e.g., getBonus()) for safer encapsulation.
Use-cases:
Educational demonstration of inheritance in Java.
Simple domain model for teaching salary calculation.
Foundation for introducing more complex HR or payroll modeling with polymorphism and abstraction.
Short key: class-bonus programmer inheritance salary.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 4b10f9c commit 53076ec
1 file changed
+7
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
6 | | - | |
7 | | - | |
| 5 | + | |
8 | 6 | | |
9 | 7 | | |
10 | | - | |
11 | | - | |
| 8 | + | |
| 9 | + | |
12 | 10 | | |
13 | 11 | | |
14 | 12 | | |
15 | 13 | | |
16 | 14 | | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
| 15 | + | |
| 16 | + | |
21 | 17 | | |
22 | 18 | | |
23 | 19 | | |
24 | | - | |
| 20 | + | |
0 commit comments