Encapsulation in Java With Question and Answer
Encapsulation in Java With Question and Answer
Create a class called Employee that includes three pieces of information as instance
variables
1. First name (type String)
2. Last name (type String)
3. Monthly salary (double).
Your class should have the following methods:
:/******************************************************************
************
********************************************************************
***********/
import java.util.Scanner; // Import the Scanner class
public class Employee {
private String firstName;
private String lastName;
private double monthlySalary;
e.setMonthlySalary(newsalary);
System.out.println("the new yearly salary of "+
e.getFirstName()+" "+e.getLastName()+" :");
System.out.println(e.getMonthlySalary()*12);
e.getMonthlySalary();
}
}
Explanation:
In the aboove code, the Employee class encapsulates the private instance variables
employee_id, employee_name, and employee_salary. The getEmployeeId() and
getEmployeeName() methods are public getter methods that allow other classes to access
employee_id and employee_name values, respectively. The setEmployeeId() and
setEmployeeName() methods are public setter methods that allow other classes to modify
employee_id and employee_name values.