Introduction
In this article, I am explaining Access Modifiers keywords in Java.
In access modifiers there are mainly the following three keywords:
- Private
- Protected
- Public
Here's the explanation:
Java Access Modifier - Private Keyword
The private keyword is an access modifier that can be applied to a method, member variable and inner class.
- Private Method
If a method marked as private, it cannot be invoked from outside the class it is declared. In other words, the private method is available to the enclosing class. For example:
- class A
- {
- private void foo()
- {
- }
- void bar()
- {
- foo();
- }
- }
- class B
- {
- void woo()
- {
- A a = new A();
- a.foo();
- }
- }
- Private Variable
The private access modifier can also be applied to member variables which are declared within a class. Like private method, a private variable can be accessed only within its enclosing class. For example:
- class A
- {
- private int number;
-
- void bar()
- {
- number = 10;
- }
- }
-
- class B
- {
- void foo()
- {
- A a = new A();
- a.number = 10.
- }
- }
- Private inner class
An inner class is one that is declared inside another class. An inner class can be declared as private thus it can be accessed only from within the enclosing class, like private method and private variable. For example:
- class A {
- private class SubA {
-
- }
- void bar() {
- SubA obj = new SubA();
- }
- }
-
- class B {
- void foo() {
- A.SubA a = new A.SubA();
- }
- }
Java Access Modifier - Protected Keyword
The protected keyword is an access modifier for method and variable of a class. When a method or a variable is marked protected, it can be accessed from the following:
- Within the enclosing class.
- Other classes in the same package as the enclosing class.
- Sub classes, regardless of packages.
The main purpose of protected keyword is to have the method or variable that can be inherited from sub classes.
Example
The following class Person declares a protected variable name, inside package p1:
- package p1;
-
- public class Person {
- protected String name;
- }
The following class in the same package can access the variable name directly:
- package p1;
-
- public class Employer {
- void hireEmployee() {
- Person p = new Person();
- p.name = "Nam";
- }
- }
The following class is in different packages but it extends the Person class so it can access the variable name directly:
- package p2;
-
- import p1.Person;
-
- class Employee extends Person {
- void doStuff() {
- name = "Bob";
- }
- }
But the following class in different package cannot access the variable name directly:
- package p2;
-
- import p1.Person;
-
- class AnotherEmployer {
- void hire() {
- Person p = new Person();
-
-
- p.name = "Nam";
- }
- }
Java Access Modifier - Public keyword
The public keyword is an access modifier for class, method and variable:
- When a class is marked public, it can be accessed from anywhere, including outside packages.
- When a method is marked public, it can be invoked not only from the enclosing class, but also from outside classes.
- When a variable is marked public, it can be accessed and updated from outside classes.
To summarize, public is the access modifier that has least restriction on the object it modifies.
The following code example shows a public class which has a public method eat() and a public variable name:
- public class Person {
- public String name;
- public void eat() {}
- }