make a class employee with a name and salary Make a class Ma

make a class employee with a name and salary. Make a class Manager
inherit from employee. Add an instance variable, named department,
of type string. Supply a method toString that prints the manager\'s name,
department and salary. Make a class executive inherit from manager.
Supply appropriate toString methods for all classes. Supply a test
program that tests these class and methods.

Solution

EmployeeTest.java


public class EmployeeTest {

  
   public static void main(String[] args) {
       Employee emp1 = new Manager(\"Suresh\", 200000, \"CSE\");
       Employee emp2 = new Executive(\"Sekhar\", 300000, \"Physics\");
       Employee emp3 = new Employee(\"Anshu\", 40000);
       System.out.println(emp1);
       System.out.println(emp2);
       System.out.println(emp3);
   }

}

Employee.java


public class Employee {
   private String name;
   private double salary;
   public Employee(String name, double salary){
       this.name = name;
       this.salary = salary;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public double getSalary() {
       return salary;
   }
   public void setSalary(double salary) {
       this.salary = salary;
   }
   public String toString(){
       return \"Name: \"+name+\" Salary: \"+salary;
   }
}

Manager.java


public class Manager extends Employee{
   private String department;
   public Manager(String name, double salary, String department){
       super(name,salary);
       this.department = department;
   }
   public String getDepartment() {
       return department;
   }
   public void setDepartment(String department) {
       this.department = department;
   }
   public String toString(){
       return super.toString()+\" Department: \"+department;
   }
}

Executive.java


public class Executive extends Manager{
   public Executive(String name, double salary, String department){
       super(name, salary,department);
   }
   public String toString(){
       return super.toString();
   }
}

Output:

Name: Suresh Salary: 200000.0 Department: CSE
Name: Sekhar Salary: 300000.0 Department: Physics
Name: Anshu Salary: 40000.0

make a class employee with a name and salary. Make a class Manager inherit from employee. Add an instance variable, named department, of type string. Supply a m
make a class employee with a name and salary. Make a class Manager inherit from employee. Add an instance variable, named department, of type string. Supply a m

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site