DEV Community

VIDHYA VARSHINI
VIDHYA VARSHINI

Posted on

Access Modifier in Java

What is access modifier: It is a keyword in Java that controls who can access a class, variable, method, or constructor. There are 4 types of access modifiers in Java. They are:
a) public
b) protected
c) default
d) private

1. public: This can be accessed from anywhere(same class, same package, different package). It is used when a class or member should be visible to all.

2. private: Accessible only within the same class. Used for data hiding and security.

3. default: Accessible only within the same package. Used when access should be restricted to the package.

4. protected: Accessible within the same package and in sub-classes of other packages. Mainly used to support inheritance.

Top comments (0)