Access modifiers

// By:Team // No Comment

Email this to someonePrint this pageShare on Google+2Share on Facebook3Tweet about this on TwitterShare on LinkedIn0Share on Reddit0Pin on Pinterest0Share on StumbleUpon2


Access modifiers

Access modifiers help you set the level of access you want for your class, variables as well as methods.

There are in all three access modifiers. But there exists a fourth one called the default access modifier . Default is an access control which will be set when one does not specify any access modifier.

Access Control:

  • Public
  • Private
  • Protected
  • Default

Access modifiers(Some or All) can be applied on Class, Variable, Methods.

 

Access Modifiers for Class

Classes in java can use only public and default access modifiers.

Public
When set to public, the given class will be accessible to all the classes available in Java world.

Default
When set to default, the given class will be accessible to the classes which are defined in the same package.

Java Access Modifiers Table for Class

Visibility Public Access Modifier Default Access Modifier
Within Same Package Yes Yes
From Outside the Same Package Yes No

Access Modifiers for Variable (Instance / Static Variable)
Variables are eligible for all of the above mentioned modifiers.

  • Default
  • Public
  • Protected
  • Private


Note*:
Visibility of the class should be checked before checking the visibility of the variable defined inside that class. If the class is visible only then the variables defined inside that class will be visible . If the class is not visible then no variable will be accessible, even if it is set to public.

Default
If a variable is set to default, it will be accessible to the classes which are defined in the same package. Any method in any class which is defined in the same package can access the variable via Inheritance or Direct access.

Public
If a variable is set to public it can be accessible from any class available in the Java world. Any method in any class can access the given variable via Inheritance or Direct access.

Protected
If a variable is set to protected inside a class, it will be accessible from its sub classes defined in the same or different package only via Inheritance.

Note:The only difference between protected and default is that protected access modifiers respect class subclass relation while default does not.

Private
A variable if defined private will be accessible only from within the class it is defined. Such variables are not accessible from outside the defined class, not even its subclass .

Java Access Modifiers Table for Variable

Visibility Public Access Modifier Private Access Modifier Protected Access Modifier Default Access Modifier
Within Same Class Yes Yes Yes Yes
From Any Class in Same Package Yes No Yes Yes
From Any Sub Class in Same Package Yes No Yes Yes
From Any Sub Class from Different Package Yes No Yes(Only By Inheritance) No
From Any Non Sub Class in Different Package Yes No No No

Access Modifiers for Methods

Methods are eligible for all of the above mentioned modifiers.

Default
When a method is set to default it will be accessible to the class which are defined in the same package. Any method in any class which is defined in the same package can access the given method via Inheritance or Direct access.

Public
When a method is set to public it will be accessible from any class available in the Java world. Any method in any class can access the given method via Inheritance or Direct access depending on class level access.

Protected
If a method is set to protected inside a class, it will be accessible from its sub classes defined in the same or different package.

Note:* The only difference between protected and default is that protected access modifiers respect class subclass relation while default does not.

Private
A method if defined private will be accessible only from within the class it is defined. Such methods are not accessible from outside the defined class, not even its subclass .

Java Access Modifiers Table for Method

Visibility Public Access Modifier Private Access Modifier Protected Access Modifier Default Access Modifier
Within Same Class Yes Yes Yes Yes
From Any Class in Same Package Yes No Yes Yes
From Any Sub Class in Same Package Yes No Yes Yes
From Any Sub Class from Different Package Yes No Yes(Only By Inheritance) No
From Any Non Sub Class in Different Package Yes No No No

Access Modifier for Local Variable
No Access Modifiers can be applied to local variables. Only final can be applied to a local variable which is a Non Access Modifer .

Difference between Inheritance or Direct Access

I will explain the difference with the help of a code mentioned below.

Super Class

Sub Class

Email this to someonePrint this pageShare on Google+2Share on Facebook3Tweet about this on TwitterShare on LinkedIn0Share on Reddit0Pin on Pinterest0Share on StumbleUpon2

About Team

Browse Archived Articles by Team

Related

Sorry. There are no related articles at this time.