---
title: "Use of Access specifiers"  
description: "Its also provide a path to control the visibility of members like classes, variables and methods. Access specifiers is used for classes, constructor,"  
author: "Hemant Patel"  
published: 2017-01-17  
updated: 2018-03-17  
canonical: https://www.mindstick.com/blog/11262/use-of-access-specifiers  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Use of Access specifiers

**[Access specifiers](https://www.mindstick.com/forum/95290/what-are-the-various-access-specifiers-for-java-classes):-** Access specifier provide a facility to restricted another class to inherit the property of parents class. Its also provide a path to [control the visibility](https://www.mindstick.com/forum/158658/what-is-the-css-property-used-to-control-the-visibility-or-hidden-state-of-an-element) of members like classes, [variables](https://www.mindstick.com/articles/715/php-variables) and methods. Access specifiers is used for classes, constructor, [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation), and method.

**There are four types of access specifiers:-**

**1.**public

**2.**private

**3.**protected

**4.**unspecified

**1.public:-I**n public access specifier we can assigned public specifier to different elements like classes, method, constructor and variable.

```
class ClassName{Public:           Int a;}int main(){ClassName obj;Obj.a=10; //Allowed} 
```

**2.private:-**In private access specifiers we can not assign private specifier to any class. its only assign to variable ,method and constructor. In other words we can not create private class.

```
Class ClassName{Protected:Int a;};Int main(){ClassName obj;Obj.a=20; // Not allowed, gives compiler error}
```

**3.protected:-**[Protected access](https://www.mindstick.com/forum/156707/what-is-the-difference-between-public-private-and-protected-access-modifiers) specifier is little bit same with private.But there is big [difference between them](https://www.mindstick.com/forum/23342/what-are-jsf-servlet-and-jsp-and-what-is-difference-between-them). In protected specifiers, [child class](https://www.mindstick.com/forum/159389/how-to-remove-child-class-from-active-div) inherit the property of parent class with in same package. but in private class the method of private class is only access in same class and child class can not access the property of parent class.

Protected access specifiers can be also used for **constructor, variable and method.**

```
class ClassName{Private:Int c;};Int main(){ClassName obj;Obj.c=10;  //NotAllowed,gives compiler error}
```

**4.Unspecified:-**Its same [as default](https://answers.mindstick.com/qa/109507/how-to-set-bing-as-default-seek-engine). When we can not use any access specifiers,then its categorized as a unspecified. All classes, method, constructor and variable that’s can not be specified with any access specifiers is fall in this category.

```
class ClassName {Int a;};int main() {ClassName obj;Obj.a=15;}            
```

---

Original Source: https://www.mindstick.com/blog/11262/use-of-access-specifiers

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
