Access control and specifiers in Java: public, private and protected

There are 3 access specifiers in Java public, private and protected. A public member can be accessed by any part of code in the program; however a private member can be accessed by only the class where it is defined. The default access specifier is public for any class. However, it is public for its package and not for other packages. A package is simply a group of classes. Java uses another access specifier protected whenever it has to use inheritance.

For Example:

Default access:

int x;

Public access:

public int y;

Private access:

private int z;

Leave a Reply