Access Modifiers in Java


access modifiers in Java are the limitations to the scope of the Java properties, basically there are four types which are described in detail in this topic.

Access modifier are some set of rules which defines what are the limits and scope of any methods or properties of a Java program. There are four type of access modifiers in the Java language specification. These are listed below in a way from least secure access to most secure access.

public
protected
defualt(also known as package protected)
private

Public is as name suggests is accessible by the whole set of programs in the application and even outside the package too. Here I am just telling you about them in brief later on in this article I'll elaborate with the help of programming examples how exactly these work.

Protected is more like the public but used mostly to use withing the boundary of the subclass, even outside the package. Protected is commonly used with the inheritance and is accessible outside the package too.

Default which I told you that is also known as package private is a more general form when you don't declare any access specifier withing any access limit it is considered as default. And this type of property or method is accessible within the limit of current or default package.

Private at last is the more secure about its protection. An attribute defined as private can not be accessed outside its class limits. these type of properties can not be inherited or accessed by even subclasses.

Not that we know the basics and theory lets get some hand on practice on these. With the help of following program.


/*
package1 and filename BaseClass.java
this will declare all four type of properties to see how these works
*/
package package1;
public class BaseClass {
    public int publicValue = 100;
    protected int protectedValue = 200;
    int defaultValue = 300;
    private int privateValue = 400;
    
    public BaseClass(){
        System.out.println("public value is :"+publicValue);
        System.out.println("protected value is :"+protectedValue);
        System.out.println("default value is :"+defaultValue);
        System.out.println("private value is :"+privateValue);
    }
    public static void main(String[] a){
        new BaseClass();
    }
}

the output will be like this:

public value is :100
protected value is :200
default value is :300
private value is :400

Now here we can see that withing the same package and same class all the four type of values are accessible however there is more to it when we add on.

Now let us have a look what happened when we try to access these in a subclass.


/*
Package package1 and filename SubClass.java
this extends the base class in same package and tries to access its attributes.
*/
package package1;
public class SubClass extends BaseClass{
    SubClass(){
        System.out.println("public value is :"+publicValue);
        System.out.println("protected value is :"+protectedValue);
        System.out.println("default value is :"+defaultValue);
        //System.out.println("private value is :"+privateValue);
    }
    public static void main(String[] args){
        new SubClass();
    }
}

When I run this I got following output,

public value is :100
protected value is :200
default value is :300

However you must have noticed that I have comment out the private variable, this is because Compiler will not allow me to compile this file successfully unless I remove this line because it is trying to access a private variable from the base class. Which according to Java rules is illegal. However I would suggest you to try uncommenting this line and compile the program to see if this works for you, which I am sure will not.

Now that we know These are all accessible withing the subclass too. Let us see if this will be accessible in Non subclass of same package also.


/*
package pacaage1, and file name : NonSubClass.java
this doesn't extends any class but uses the object reference to access the properties of BaseClass.
*/
package package1;

public class NonSubClass {
    NonSubClass(){
        BaseClass bc = new BaseClass();
        System.out.println("public value is :"+bc.publicValue);
        System.out.println("protected value is :"+bc.protectedValue);
        System.out.println("default value is :"+bc.defaultValue);
        //System.out.println("private value is :"+bc.privateValue);
    }
    public static void main(String[] args){
        new NonSubClass();
    }
}

In the output there is not much of the difference, see below:

public value is :100
protected value is :200
default value is :300
private value is :400
public value is :100
protected value is :200
default value is :300

You might be wondering why It repeating the output, well my friend this is because we are calling the BaseClass constructor when creating an object and when the constructor is called it executes the code inside it, which you may see in the class definition above. There is nothing to tell so I am skipping on to next class now.

Lets make a new package and name it package2. This will help us understand the default and protected in more details. look at the class definition below


/*
package name: package2 and filename: SubClass.
This is a subclass inside another package or say outside the BaseClass pacakge.
*/

package package2;
import package1.BaseClass;
public class SubClass extends BaseClass{
    SubClass(){
        System.out.println("public value is :"+publicValue);
        System.out.println("protected value is :"+protectedValue);
        //System.out.println("default value is :"+defaultValue);
        //System.out.println("private value is :"+privateValue);
    }
    public static void main(String[] args){
        new SubClass();
    }
}

the output is as follows:
public value is :100
protected value is :200
default value is :300
private value is :400
public value is :100
protected value is :200

Now again, don't worry about the values from the BaseClass constructor as when the SubClass constructor is called this will automatically invoke the super() constructor and again I have commented last two entries because default is also not allowed outside package and this is another package. Again, I suggest you try to remove the comment and try recompiling to understand better.

Now we are at our final stage of testing, Look here, in the NonSubClass outside the package1.


/*
package name: package2 and filename : NonSubClass.java
this is a non subclass outside the package i.e. package1.
*/
package package2;
import package1.BaseClass;
public class NonSubClass {
    NonSubClass(){
        BaseClass bc = new BaseClass();
        System.out.println("public value is :"+bc.publicValue);
        //System.out.println("protected value is :"+bc.protectedValue);
        //System.out.println("default value is :"+bc.defaultValue);
        //System.out.println("private value is :"+bc.privateValue);
    }
    public static void main(String[] args){
        new NonSubClass();
    }
}

here is the output:
public value is :100
protected value is :200
default value is :300
private value is :400
public value is :100

again you'll see that There are all three entries commented except the public one. Because if the class is non sub class this will not let any other access type work.

I think this will be enough to clarify the access types in Java.

Note: if you are trying to access a class from one package to another you need to import the package or the class before using it.