In this Java Tutorial, you will learn what access modifiers are in Java, how modifiers are helpful in controlling the access to a class or members of a class.
Access Modifiers in Java
There are two types in access modifiers
- Class Level Modifiers – These modifiers control access to a class.
- public
- default
- Class-Member Level Modifiers – These modifiers control access to the members of a class: methods and properties.
- public
- private
- protected
- default
Class Level Modifiers
The syntax to specify a modifier for Class is as shown below.
modifier class ClassName{ ... }
modifier | could be public / default |
class | is the standard keyword to define a class |
ClassName | is the name of the class |
1. public – Class Level Modifier
The modifier, public, makes the class visible/accessible to the classes everywhere. A sample code snippet to make a class, which is accessible to public, is shown below:
public class MyClass{
...
}
2. default – Class Level Modifier
By default, if no modifier is given to a class, its visible only to the classes present in same package as that of the class. A sample code snippet to make a class, which is accessible by default only to classes in the same path as of itself, is shown below:
class MyClass{
...
}
Diagrammatic Representation of Class Modifiers in Java
The following diagram depicts the public, default accessibility of SampleClass.java.
- If class definition of SampleClass.java is public class SampleClass{ }, then any of the classes ClassA, ClassB, ClassC, ClassD or any other class could access SampleClass.
- If class definition of SampleClass.java is class SampleClass{ }, then only ClassA and ClassB could access SampleClass.
Class Member Level Modifiers
Going forward, we refer variables and methods of a Class as Class Members.
Syntax
The syntax for defining a variable with access modifier is
access_modifier data_type variable_name [ = initial value];
Example: public int i = 10;
The syntax for defining a method with access modifier is
access_modifier return_type method_name(arguements){ ... }
Example: public int getPrimeNumer(int nth){..};
Overview of Class Member Level Access Modifiers
Following table gives an overview of Accessibility to different scopes for Class Member Level Access Modifiers.
Access to | public | protected | default | private |
---|---|---|---|---|
Other members of the same class | Yes | Yes | Yes | Yes |
Classes in the same package | Yes | Yes | Yes | No |
Its Sub-classes | Yes | Yes | No | No |
Classes in other packages | Yes | No | No | No |
1. public
If a class member is declared public, then the class member is accessible to :
- Other members of the same class
- Classes in the same package
- Its Sub-classes
- Classes in other packages
The following example shows how to make class members public.
The variables/properties: age, name are made public using public
keyword in the declaration/initialization. Also, the method: getEntityInfo() is made public using public keyword in the definition of this method.
MyClass.java
public class MyClass {
// variables that are public
public int age = 2;
public String name = "TutorialKart.com";
/**
* This method is declared public.
* @return Persons information
*/
public String getEntityInfo(){
return name+" is "+age+" years old.";
}
public static void main(String[] args) {
System.out.println(new AccessModifiers().getEntityInfo());
}
}
2. protected
If a class member is declared protected, then the class member is accessible to
- Other members of the same class
- Classes in the same package
- Its Sub-classes
Classes in other packages
The following example shows how to make class members protected.
The variables/properties: age, name are made protected using protected
keyword in the declaration/initialization. Also, the method: getEntityInfo() is made protected using protected keyword in the definition of this method.
MyClass.java
public class MyClass {
// variables that are protected
protected int age = 2;
protected String name = "TutorialKart.com";
/**
* This method is declared protected.
* @return Persons information
*/
protected String getEntityInfo(){
return name+" is "+age+" years old.";
}
public static void main(String[] args) {
System.out.println(new AccessModifiers().getEntityInfo());
}
}
3. default
If no modifier is speicified, default access is given to the class member. If a class member is declared default, then the class member is accessible to
- Other members of the same class
- Classes in the same package
Its Sub-classesClasses in other packages
The following example java program shows how to give default access to class members.
The variables/properties: age, name and the method getEntityInfo() does not have any modifier specified. This means these class members will give access to only other members of the same class or classes in the same package.
MyClass.java
public class MyClass {
// variables that have no access modifier
int age = 2;
String name = "TutorialKart.com";
/**
* This method is declared with default access modifier.
* @return Persons information
*/
String getEntityInfo(){
return name+" is "+age+" years old.";
}
public static void main(String[] args) {
System.out.println(new AccessModifiers().getEntityInfo());
}
}
4. private
If a class member is declared private,
then the class member is accessible to :
- Other members of the same class
Classes in the same packageIts Sub-classesClasses in other packages
The following example shows how to give private access to class members.
The class members: age, name and getEntityInfo() are made private using private
keyword in the declaration/initialization. Only members of MyClass
class will have access these private class members.
MyClass.java
public class MyClass {
// variables that are private
private int age = 2;
private String name = "TutorialKart.com";
/**
* This method is declared private.
* @return Persons information
*/
private String getEntityInfo(){
return name+" is "+age+" years old.";
}
public static void main(String[] args) {
System.out.println(new AccessModifiers().getEntityInfo());
}
}
Conclusion
In this Java tutorial, we have learned Access Modifiers in Java. In our next tutorial, we shall learn Arithmetic Operators and Relational Operators in Java.