Lab 3
Lab 3
Using the concept of inheritance and run time polymorphism write a program for the
following.
A company has three employees(boss, manager and clerk). Each of them has name, address and
salary. All of the employees need to display name address and salary. But manager has extra
feature bonus. Also calculate the total salary of the manager after adding bonus and print the
total amount. Note all of the employees have get_name_address(), get_salary() function. But
manager has extra function set_bonus() too.
code:
class Employee {
private String name;
private String address;
double salary;
@Override
public void getSalary() {
super.getSalary();
System.out.println("Bonus: " + bonus);
double totalSalary = super.salary + bonus;
System.out.println("Total Salary (including bonus): " +
totalSalary);
}
}
System.out.println("Details of Boss:");
boss.getNameAddress();
boss.getSalary();
System.out.println();
System.out.println("Details of Manager:");
manager.getNameAddress();
manager.setBonus(10000.0);
manager.getSalary();
System.out.println();
System.out.println("Details of Clerk:");
clerk.getNameAddress();
clerk.getSalary();
}}
Output:
Details of Boss:
Name: Samjhana Rokaya
Address: Bardiya
Salary: 80000.0
Details of Manager:
Name: Gaurav Chhetri
Address: Kathmandu
Salary: 60000.0
Bonus: 10000.0
Total Salary (including bonus): 70000.0
Details of Clerk:
Name: Rajan
Address: Pokhara
Salary: 40000.0
Code:
abstract class Bank{
abstract int getRateOfInterest();
}
class EBL extends Bank{
int getRateOfInterest(){return 7;}
}
class NIBL extends Bank{
int getRateOfInterest(){return 8;}
}
class q2{
public static void main(String args[]){
Bank b;
b=new EBL();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new NIBL();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
}}
Output:
Rate of Interest is: 7 %
Rate of Interest is: 8 %