Visibility Modes (Access Specifier) in Class

Visibility Modes/Access Specifier in Java

Access Specifier defines the visibility of the Property/Fields/Variable/Methods of a class. They are the label/prefix before data type of the property. like

private int rollno; // here private is the Access Specifier.

In Java there are Four Access Specifiers those define the visibility of the property.

1)Private : Private property of the class can be accessed from class it self only. Means Other member functions of the class can use this type of variables. Mostly variables are declared with private visibility. It provides better security to the class variables.

2)Protected : This type of access specifier is useful only when you use Inheritance. (We will cover inheritance in other posts). Protected members of the class can be accessed from class it self + child classes of the class.

3)Default : Default access specifier in not a key word, when you do not specify any access specifier then it will be default. Default class members can be used only within Same Pacakge. (concept of package we will discuss later)

4)Public : Public members of the class can be referred from any other class and from any other package also. It is the specifier that is used for Constructor and the methods those are used from outside the class.

class DemoClass{
private int only_same_class;
protected int child_can_use;
int any_class_in_package; //default
public int any_class;

Leave a Reply

Your email address will not be published. Required fields are marked *

× How can I help you?