Quiz 2
(Inheritance & Polymorphism)
Question 1: You are tasked with developing a small application for a company that calculates
the annual bonuses for different types of employees. The company has three types of employees:
Managers, Engineers, and Interns. Each type of employee receives a different percentage of their
salary as a bonus:
Managers receive 10% of their salary as a bonus.
Engineers receive 8% of their salary as a bonus.
Interns receive 2% of their salary as a bonus.
Instructions:
1. Create a base class Employee that has:
o A constructor to initialize the employee's name and salary.
o A method calculateBonus() that returns the bonus amount (default to 5% of the
salary).
o A method displayBonus() that prints the employee's name and their bonus
amount.
2. Create three subclasses:
o Manager that overrides the calculateBonus() method to return 10% of the
salary.
o Engineer that overrides the calculateBonus() method to return 8% of the
salary.
o Intern that overrides the calculateBonus() method to return 2% of the salary.
3. In the main method of your program, create one object of each subclass (Manager,
Engineer, and Intern), and call the displayBonus() method for each to print their
respective bonus.
MOHAMMAD JAHANGEER(25592)
SOURCE CODE:
class Employee {
String name;
double salary;
Employee(String name, double salary) {
this.name = name;
this.salary = salary;
double calculateBonus() {
return 0.05 * salary;
void displayBonus() {
double bonus = calculateBonus();
System.out.printf("%s receives a bonus of Rs%.2f%n", name, bonus);
class Manager extends Employee {
Manager(String name, double salary) {
super(name, salary);
@Override
double calculateBonus() {
return 0.10 * salary;
class Engineer extends Employee {
Engineer(String name, double salary) {
super(name, salary);
@Override
double calculateBonus() {
return 0.08 * salary;
class Intern extends Employee {
Intern(String name, double salary) {
super(name, salary);
@Override
double calculateBonus() {
return 0.02 * salary;
public class Main {
public static void main(String[] args) {
Employee manager = new Manager("Jahangeer", 90000);
Employee engineer = new Engineer("zain", 75000);
Employee intern = new Intern("uzair", 30000);
manager.displayBonus();
engineer.displayBonus();
intern.displayBonus();
OUTPUT