-4

I have to create employee object. There is an attribute called occupation in that employee class. Only accountant's can access the method call calculatnetsalary() method. How to code this in java.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Duleep
  • 1
  • 6
  • Possibly this is something you cannot do in Java. If `calculatnetsalary()` is in the same class as `accountant` then you can make the method private. – markspace Mar 20 '18 at 19:02
  • Possible duplicate of [Java: How to limit access of a method to a specific class?](https://stackoverflow.com/questions/11549394/java-how-to-limit-access-of-a-method-to-a-specific-class) – Przemysław Moskal Mar 20 '18 at 19:13

1 Answers1

0

There are a couple of ways to achieve this.

Maybe the best approach is to put in the same package your accountant and employee classes and make the calculatenetsalary method package protected. Of course if you don't want any other class to access the method, the other classes will go to different packages.

balsick
  • 1,099
  • 1
  • 10
  • 23