Java Program for Employee Details using Class and Object

In this article, we will talk about the Java program for employee details using class and object.

An Employee is a person or also be referred to as an entity that consists of various attributes such as – emp_id, emp_name, emp_salary, emp_department, emp_email, emp_address, and many more. We use the getter (to receive Employee details) and setter (to set Employee details) method in this program.

In the main class, we will create an object of the EmployeeDetails class and with the help of an object, we will access the attributes of the EmployeeDetails class.

Program:

Output:

Explanation:

In this program, we defined two classes named ‘EmployeeDetails’ and ‘Employee’. In the class ‘EmployeeDetails’, we define some variables which represent the employee’s properties. These variables are as follows:

emp_id, emp_salary are of int type whereas

emp_address, emp_department, emp_email, emp_name are of String type..

In this EmployeeDetails class, we created getter and setter functions for each property of the employees to fetch and assign the values. In the ‘Employee’ class, we created an object of the EmployeeDetails class. Now, we assign the values using the setter method. And call these setter methods using objects. We also created one function namely: ‘toString()’. This function invokes when we print the class’s object. In the next line, we print the object of EmployeeDetails class so it prints the string which is return by the ‘toString()’ method.

We declare some more variables named ‘sal’ and ‘increment’. We get the emp_salary value using its getter method and initialize it to the sal variable. And, initialize increment as 0. Now, we start incrementing the salary of the employees based on some conditions. And, after incrementing salary, we again print the employee’s details using the toString() method.

So, that’s all about the Java program for employee details using class and object with detailed explanation.

Leave a Reply

Your email address will not be published. Required fields are marked *