The Employee class will contain a String attribute for an employee’s name and a double attribute for the employee’s salary. Which of the following is the most appropriate implementation of the class?

a. public class Employee

{
public String name;
private double salary;
// constructor and methods not shown
}

b. public class Employee
{
private String name;
private double salary;
// constructor and methods not shown
}

c. private class Employee
{
public String name;
public double salary;
// constructor and methods not shown
}

d. private class Employee
{
private String name;
private double salary;
// constructor and methods not shown
}

Respuesta :

Answer:

The answer is "Option b".

Explanation:

In the choice b, a public class "Employee" is declared, in which two private variables "name and salary" is declared whose data type is a string and double. At this, the class is accessible outside the scope but it variable accessible in the class only and the wrong choice can be defined as follows:

  • In choice a, datatypes access modifiers were different that's why it is wrong.
  • In choice c and d both classes use a priavte access modifier, which means it can't accessible outside that's why it is wrong.