Employee details program in java using class and object

Employee details program in java as below.

Here two classes are created

  1. Employee class
  2. EmployeeMain class

Details of these two classes are as below

java program for employee details using class and object

employee class in java : Employee.java

This class contains various fields of employees like id, fname, lname, address etc.

Its corresponding getter and setter method are created in the program.

Employee.java is a java bean and its good example of java encapsulation.

fields of Employee object can not be directly accessible outside the class to access this we need getter and setter methods.

Default and parameterized constructors are created to initialize employee objects.

toString() of the Object class is overridden to convert the object to String.

Some fields like hra, providentFund and grossSalary are calculated from basicSalary.

So user provides only basicSalary value and other derived values are calculated based on it.

Two different methods are used to initialize values(Parameterised constructor and setter methods).

So the calculation of derived values is done inside the constructor and in the setter method.

Providing addDate value is optional for a user it the user does not provide this value constructor or setter method does this.

EmployeeMain.java

This class contain public static void main() (java main method) inside main tho objects of Employee, class is created.

The first object is created using parameterized constructor and the object’s value is a printer by overriding the toString() Object class.

The second object is created using the default constructor of employee and values are assigned using setter methods of the class.

To print the value of the object getter method is used. Instead of using the getter method toString() can also be used.

It’s up to use which method he wants to use to set values in the object.

The result of the above program is

Employee details

If anyone wants to change the value of any field then the appropriate setter method needs to call

like want to update lname of employee object then use

employee.setLname("Verma");

this will assign a new value to lname.

Hope you understand how to show the Employee details program in java

Read More

  1. Database operations with employee details
  2. Constructors in Java- Type Overloading and examples

Employee Management Project in JSP Servlet