Question 7.3: Create a class named 'Member' having the following members:
Data members
1 - Name
2 - Age
3 - Phone number
4 - Address
5 - Salary
It also has a method named 'printSalary' which prints the salary of the members.
Two classes 'Employee' and 'Manager' inherits the 'Member' class. The 'Employee' and 'Manager' classes have data members 'specialization' and 'department' respectively. Now, assign name, age, phone number, address and salary to an employee and a manager by making an object of both of these classes and print the same.
Solution:
//Employee class
package com.company;
public class Employee extends Member {
String specialization;
String departmant;
public void display(){
System.out.println("Name: "+ name);
System.out.println("Age: "+age);
System.out.println("Phone number: "+pheoneNamber);
System.out.println("Adress: "+adress);
printsalary();
System.out.println("specialization: "+specialization);
System.out.println("Departmant: "+departmant);
}
}
//Main class
package com.company;
public class Main {
public static void main(String[] args) {
Manager member = new Manager();
member.name = "Sifat";
member.age = 20;
member.salary = 20000.00;
member.adress ="Dhaka";
member.pheoneNamber ="01723903205";
member.specialization="programer";
member.departmant="CSE";
member.display();
System.out.println();
System.out.println();
Employee employee = new Employee();
employee.name = "Maksud";
employee.age = 22;
employee.salary = 10000.00;
employee.adress ="Dhaka";
employee.pheoneNamber ="01723903205";
employee.specialization="UX editor";
employee.departmant="CSE";
employee.display();
}
}
//Manager class
package com.company;
public class Manager extends Member {
public String specialization;
String departmant;
public void display(){
System.out.println("Name: "+ name);
System.out.println("Age: "+age);
System.out.println("Phone number: "+pheoneNamber);
System.out.println("Adress: "+adress);
printsalary();
System.out.println("specialization: "+specialization);
System.out.println("Departmant: "+departmant);
}
}
//Member class
package com.company;
public class Member {
String name;
int age;
String pheoneNamber;
String adress;
double salary;
public void printsalary(){
System.out.println("Salary "+salary);
}
}