Java Access Modifiers

Access modifiers in java is used to grant access to the class, methods, variables, constructors, interface etc based on the access modifier provide to it.

In java there are 4 different types of access modifiers. They are,

  • Public

  • Private

  • Protected

  • Default

Public access modifier

The public modifier will provide access within a class, outside the class, within the package and between the packages.

Example for public modifier

class Animal {
    // public variable
    public int legs;

    // public method
    public void display() {
        System.out.println("I am an animal.");
        System.out.printf("I have %d legs.",legs);
    }
}

// Main.java
public class Main {
    public static void main( String[] args ) {
        // accessing the public class
        Animal dog = new Animal();

        // accessing the public variable
        dog.legs = 4;
        // accessing the public method
        dog.display();
    }
}

Output

I am an animal.
I have 4 legs.

In the above program, we have a class Animal with a variable legs and a function display() both with the public access specifier.

In the Main class, we create an object for the Animal class and access the legs and display() function of the class since they are declared public.

Private Modifier

The private modifier will provide access only within the class.
Any variables or methods that are declared as private, cannot be accessed outside the class with the object of the class.

Example for private modifier

class Employee {
    // private variable
    private String fullName;

    // public method
    public void SetEmployeeName(String name) {
        fullName = name;
    }
    
    // public method
    public String GetEmployeeName(){
        return fullName;
    }
}

// Main.java
public class Main {
    public static void main( String[] args ) {
        // accessing the public class
        Employee employee = new Employee();
    
        employee.SetEmployeeName("Andrew");
        System.out.println("Employee Name is "+ employee.GetEmployeeName());
    }
}

Output

Employee Name is Andrew

In the above program, we have declared a class Employee with a private variable fullName.

The fullName variable cannot be accessed by the object of the Employee class. So we have 2 public functions to set and get the value of the fullName.

The SetEmployeeName() function is used to set the value of the fullName variable and GetEmployeeName() function is used to return the value of fullName variable.

If we try to set value to fullName variable with the object of the Employee class like

employee.fullName = "Andrew";

then it will throw an error like,

Main.java:21: error: fullName has private access in Employee
        employee.fullName = "Andrew";

Protected Access Modifier

The protected modifier will provide access within the package and all of its sub-classes.

Example for protected access modifier

class Animal {
    protected int legs;

    protected void display() {
        System.out.println("I am an animal.");
        System.out.printf("I have %d legs.",legs);
    }
}

// Main.java
public class Main extends Animal {
    public static void main( String[] args ) {
        // accessing the public class
        Main dog = new Main();

        // accessing the public variable
        dog.legs = 4;
        // accessing the public method
        dog.display();
    }
}

Output

I am an animal.
I have 4 legs.

In the above example, we have a class Animal with legs variable and display() function with protected type.

Then the Animal class is inherited by the Main class. Then we create the dog object for the Main class and access the legs variable and display() function of Animal class.

It is not possible to create a class or an interface as protected.

Default Access Modifier

The default modifier provides access through out the package.

Example for default acccess modifier

package employeePackage;
class Employee {
    public String fullName;
    Employee(String name){
        fullName = name;
    }
    void PrintName(){
        System.out.println(fullName);
    }
}

Any class or method or variable that does not have access modifier will be considered as default access modifier. Here the class Employee will be considered as default access modifer, and the Employee class can be accessed by all the classes within this employeePackage.

If we try to access Employee class in any other package then it will throw an error.


Most Read