0% found this document useful (0 votes)
17 views4 pages

Lab 3

java
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)
17 views4 pages

Lab 3

java
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/ 4

1.

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;

public Employee(String name, String address, double salary) {


this.name = name;
this.address = address;
this.salary = salary;
}

public void getNameAddress() {


System.out.println("Name: " + name);
System.out.println("Address: " + address);
}

public void getSalary() {


System.out.println("Salary: " + salary);
}
}

class Manager extends Employee {


private double bonus;

public Manager(String name, String address, double salary) {


super(name, address, salary);
this.bonus = 0.0;
}

public void setBonus(double bonus) {


this.bonus = bonus;
}

@Override
public void getSalary() {
super.getSalary();
System.out.println("Bonus: " + bonus);
double totalSalary = super.salary + bonus;
System.out.println("Total Salary (including bonus): " +
totalSalary);
}
}

class Boss extends Employee {


public Boss(String name, String address, double salary) {
super(name, address, salary);
}
}

class Clerk extends Employee {


public Clerk(String name, String address, double salary) {
super(name, address, salary);
}
}
public class Main {
public static void main(String[] args) {
Boss boss = new Boss("Samjhana Rana", "Bardiya", 80000.0);
Manager manager = new Manager("Gaurav Chhetri", "Kathmandu", 60000.0);
Clerk clerk = new Clerk("Rajan", "Pokhara", 40000.0);

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

3.Write a program to show the implementation of abstract class.

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 %

You might also like