In this tutorial, we’ll build a simple Java application for managing employment details. We’ll create classes for employees, managers, and departments, and explain each step with comments for beginners to understand.
1. Employee Class
/**
* The Employee class represents individual employees in the organization.
*/
public class Employee {
// Attributes
private int id; // Unique identifier for each employee
private String name; // Name of the employee
private double salary; // Salary of the employee
// Constructor
public Employee(int id, String name, double salary) {
// Initialize attributes with provided values
this.id = id;
this.name = name;
this.salary = salary;
}
// Getters and Setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
// Method to display employee details
public void displayDetails() {
System.out.println("Employee ID: " + id);
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
}
}
Explanation:
- The
Employee
class represents individual employees in the organization. - It has three attributes:
id
,name
, andsalary
to store employee information. - The constructor method
Employee
initializes these attributes when a new employee object is created. - Getters (
getId
,getName
,getSalary
) and setters (setId
,setName
,setSalary
) are provided to access and modify the attributes. - The
displayDetails
method prints the employee’s ID, name, and salary to the console.
2. Manager Class
/**
* The Manager class represents managers in the organization.
* It extends the Employee class.
*/
public class Manager extends Employee {
// Additional attribute
private String department; // Department managed by the manager
// Constructor
public Manager(int id, String name, double salary, String department) {
// Call the constructor of the superclass (Employee) to initialize inherited attributes
super(id, name, salary);
// Initialize additional attribute specific to managers
this.department = department;
}
// Getter and Setter for department
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
// Overriding displayDetails method to include department
@Override
public void displayDetails() {
// Call the displayDetails method of the superclass (Employee) to print employee details
super.displayDetails();
// Print additional details specific to managers
System.out.println("Department: " + department);
}
}
Explanation:
- The
Manager
class represents managers in the organization and extends theEmployee
class. - It adds an extra attribute
department
to store the department managed by the manager. - The constructor method
Manager
initializes all attributes, including those inherited from theEmployee
class. - Getter and setter methods are provided for the
department
attribute. - The
displayDetails
method overrides the method in the superclass to include the department information when displaying manager details.
3. Department Class
import java.util.ArrayList;
import java.util.List;
/**
* The Department class represents departments in the organization.
*/
public class Department {
// Attributes
private String name; // Name of the department
private List<Employee> employees; // List of employees in the department
// Constructor
public Department(String name) {
// Initialize department name
this.name = name;
// Initialize list to store employees
employees = new ArrayList<>();
}
// Getter and Setter for name
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// Method to add an employee to the department
public void addEmployee(Employee employee) {
// Add employee to the list
employees.add(employee);
}
// Method to display department details
public void displayDetails() {
// Print department name
System.out.println("Department Name: " + name);
// Iterate through the list of employees and print their details
System.out.println("Employees:");
for (Employee emp : employees) {
emp.displayDetails();
}
}
}
Explanation:
- The
Department
class represents departments in the organization. - It has two attributes:
name
to store the department name andemployees
to store a list of employees in the department. - The constructor method
Department
initializes the department name and creates an empty list of employees. - Getter and setter methods are provided for the
name
attribute. - The
addEmployee
method adds an employee to the department’s list of employees. - The
displayDetails
method prints the department name and the details of all employees in the department.
Written By
Welcome to DevTechTutor.com, your ultimate resource for mastering web development and technology! Whether you're a beginner eager to dive into coding or an experienced developer looking to sharpen your skills, DevTechTutor.com is here to guide you every step of the way. Our mission is to make learning web development accessible, engaging, and effective.
Other Articles
Previous