Skip to content

Commit 53076ec

Browse files
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

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1-
class Bonus
2-
{
1+
class Bonus {
32
int bonus = 10000;
43
}
54

6-
class Programmer extends Bonus
7-
{
5+
class Programmer extends Bonus {
86
int basicsal = 50000;
97
int totsal;
10-
void salary()
11-
{
8+
9+
void salary() {
1210
totsal = basicsal + bonus;
1311
System.out.println(totsal);
1412
}
1513
}
1614

17-
class InheritBonus
18-
{
19-
public static void main(String[] args)
20-
{
15+
class InheritBonus {
16+
public static void main(String[] args) {
2117
Programmer p = new Programmer();
2218
p.salary();
2319
}
24-
}
20+
}

0 commit comments

Comments
 (0)