asked 110k views
0 votes
Determine the best access modifier for each of the following situations: a. A class Employee records the name, address, salary, and phone number. b. An adding method inside of a class BasicMath.

asked
User Shinese
by
8.1k points

1 Answer

3 votes

Answer:

a. Protected

b. Public

Step-by-step explanation:

There are four acess modifier in Java.

Default: Acessible only within the same package.

Public: Can be acessed by any class.

Private: Acessible only within the class.

For example, you have a class employee and a private method. This method can only be accessed by an object that is an instance of an employee.

Protected: Used in classes that extend each other. For example, a class of employees would extend employee.

So:

a. A class Employee records the name, address, salary, and phone number.

The best acesses modifier is protected. A class may extended employee but have the same arguments(name, adress, salary, phone number), so it should also have acess to the method.

b. An adding method inside of a class BasicMath.

This method can be used in a variety of packages and projects and classes... and there is no important information regarding security. So the best method is public.

answered
User Menfon
by
8.5k points

No related questions found