0% found this document useful (0 votes)
5 views5 pages

Access Modifiers in Java

Uploaded by

sariwanewman62
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Access Modifiers in Java

Uploaded by

sariwanewman62
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Access Modifiers in Java

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

There are three access modifiers. Not including 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 the 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 Instance & Static Variables


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 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 in which it is defined.
Such variables are not accessible from outside the defined Class, not even in its subclass .

Java Access Modifiers Table for Variable


Public Access Private Access Protected Access Default Access
Visibility
Modifier Modifier Modifier Modifier
Within Same Class Yes Yes Yes Yes
From Any Class in Same
Yes No Yes Yes
Package
From Any Sub Class in
Yes No Yes Yes
Same Package
From Any Sub Class Yes(Only By
Yes No No
from Different Package Inheritance)
From Any Non Sub Class
Yes No No No
in Different Package
Access Modifiers for Methods
Methods are eligible for all of the following modifiers.

Default

When a Method 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 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 that is defined private will be accessible only from within the Class in which it is
defined. Such Methods are not accessible from outside the defined Class, not even its subclass .

Java Access Modifiers Table for Method


Public Access Private Access Protected Access Default Access
Visibility
Modifier Modifier Modifier Modifier
Within Same Class Yes Yes Yes Yes
From Any Class in Same
Yes No Yes Yes
Package
From Any Sub Class in
Yes No Yes Yes
Same Package
From Any Sub Class Yes(Only By
Yes No No
from Different Package Inheritance)
From Any Non Sub Class
Yes No No No
in Different Package
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
Below is illustrated the difference between inheritance and direct access.

Super Class

1
2 package jbt1;
3
4 public class FirstClass {
5 public int i;
6 protected int j;
7 private int k;
8
9 }
10

Sub Class

1
2 package jbt;
3
4 import jbt1.FirstClass;
5
6 class SecondClass extends FirstClass {
7
8 void method() {
9 System.out.println(i);
10
11 /*
12 * Here you are trying to access protected variable directly. So it will
13 * not be accessible and compile will give an error.
14 */
15
16
System.out.println(j);
17
18
/*
19
* As k is private so it will not be accisible to subclass neither way.
20
* Neither it can be accessed via Inheritance nor direct.
21
*/
22
System.out.println(k); // Compilation Error
23
FirstClass cls = new FirstClass();
24
25
/*
26
* Here property j is accessed via Inheritance hence it will be
27
* accessible. But same variable can not be accessed if you try to
28
* access via instance because modifier used here is protected so it
29
* will be available to sub class only via inheritance.
30
*/
31
32
System.out.println(cls.j);
33
34
// Private variable will not be accessible here also.
35
System.out.println(cls.k); // Compilation error
36
}
37
}
38
39

You might also like