# Access Modifiers

&#x20;Access modifiers help you set the level of access you want for your Class, variables as well as Methods.

&#x20;The access modifiers in java specify accessibility (scope) of a data member, method, constructor or class.&#x20;

#### There are 4 types of java access modifiers:

&#x20;1\. Private

&#x20;2\. Default

&#x20;3\. Protected&#x20;

4\. public

#### &#x20;1) private access modifier&#x20;

The private access modifier is accessible only within class. Simple example of private access modifier In this example, we have created two classes A and Simple. A class contains private data member and private method. We are accessing these private members from outside the class, so there is compile time error.

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfXXDiodXs-xhsYzVK%2F-LTfY79U9lkXu-TyeUhn%2Fimage.png?alt=media\&token=f0e8a712-0a02-4ca6-8afb-b8ca71981324)

#### &#x20;2) default access modifier

&#x20;If you don't use any modifier, it is treated as default by default. The default modifier is accessible only within package.

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfXXDiodXs-xhsYzVK%2F-LTfYJNVY5Xs5uI0wKpN%2Fimage.png?alt=media\&token=8f6367df-ff09-4bed-aed6-41b194f4bad6)

#### &#x20;3) protected access modifier

#### &#x20;The protected access modifier is accessible within package and outside the package but through inheritance only. **The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class.**

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfXXDiodXs-xhsYzVK%2F-LTfYX_x02dA9TKRUeK1%2Fimage.png?alt=media\&token=0f7e13ff-ddde-44ba-b33c-68660e06ceb1)

#### &#x20;4) public access modifier

&#x20;The public access modifier is accessible everywhere. It has the widest scope among all other modifiers.

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfXXDiodXs-xhsYzVK%2F-LTfYgPQUnXdl5rNtXoF%2Fimage.png?alt=media\&token=b5be7faa-e208-4009-8a89-f9998d6305ff)

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfXXDiodXs-xhsYzVK%2F-LTfYmHA_51NPpKUkyQl%2Fimage.png?alt=media\&token=e4580662-f7e0-4652-9ca2-fb9a8b66c2df)

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTfXXDiodXs-xhsYzVK%2F-LTfYqL0uLHs-mCJoOXv%2Fimage.png?alt=media\&token=3f8fc82c-d9a8-4ce3-bf5b-120223b1ca0e)

