0% found this document useful (0 votes)
3 views

Question 5

Uploaded by

sarchingriichigo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Question 5

Uploaded by

sarchingriichigo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Q.5.

Write a Java program to implement multi-level inheritance by considering the


following details:
a. Employee (base class containing employee details)
b. Account (a class that extends employee and includes account information)
c. Salary (a class that exztends the account and calculates the salary)
Code:

class Employee {
int id;
String name;

public void setEmployee(int id, String name) {


this.id = id;
this.name = name;
}

public void displayEmployee() {


System.out.println("Employee Name:" + this.name);
System.out.println("Employee ID:" + this.id);
}
}

class Account extends Employee {


String accountNumber;

public void setAccount(String accountNumber) {


this.accountNumber = accountNumber;
}

public void displayAccount() {


System.out.println("Account Number: " + this.accountNumber);
}
}

class Salary extends Account {


double basicPay, deductions;

public void setSalaryDetails(double basicPay, double deductions)


{
this.basicPay = basicPay;
this.deductions = deductions;
}

public double calculateNetSalary() {


return basicPay - deductions;
}
public void displaySalary() {
System.out.println("Basic Pay: " + basicPay);
System.out.println("Deductions: " + deductions);
System.out.println("Net Salary: " + calculateNetSalary());
}
}

class MultiLevelInheritance {
public static void main(String[] args) {
Salary emp = new Salary();

emp.setEmployee(101, "Sagar Tamang");


emp.setAccount("ACC101");
emp.setSalaryDetails(50000, 5000);

emp.displayEmployee();
emp.displayAccount();
emp.displaySalary();
}
}

Output:

You might also like