Access modifiers in java

The Access Modifiers in java help to control the access or scope of a class, variable, method, and constructor.

Ex. Say we have declared a variable then using an access modifier we will come to know at what all places we can access this variable. We can access the variable within a class or within a package or outside a package. This is all decided by the access modifier we use.

Types of Access modifiers

There are 4 types of access modifiers in java

1 ) Private
2 ) Default
3 ) Protected
4 ) Public

Private access modifier

We can declare a member as private using the private keyword. We can access private members within a class. They cannot be accessed outside of a class.

Visibility: Within a class

We can declare a variable, method, and constructor as private. But we cannot make a top-level class private.

Why a top-level class cannot be private?

Whenever we create a java application or a project we will be creating many classes. These classes are having some business importance and the classes will be linked to each other. Since the classes are linked to each other so there is no point in making them private.

Ex. Say we are creating a Student Management project. Now this project can contain classes like Student( which stores the student info ), Subject, etc. Here we will have some relation between Student class and Subject class. If we make the Subject class private then can it be used in the Student class? No. If we are not able to use a class then there is no point in creating it hence top-level cannot be declared as private.

Private access modifier example

package com.getinputs;

class Student {

  private int rollNo;

  private String name;
}

public class Program {

  public static void main(String[] args) {

    Student student = new Student();

    student.name = "Suraj";

  }
}

CE: Exception in thread "main" java.lang.Error: Unresolved compilation problem: The field Student.name is not visible

Note* In the above example we are trying to access the name variable of the Student class in the Program class. Since the name is a having a private access modifier hence it is not accessible outside of the Student class and we are getting a compile-time exception.

How can we access the private variables outside of a class?

By creating the public getter and setter methods.

package com.getinputs;

class Student {

  private int rollNo;

  private String name;

  public int getRollNo() {
    return rollNo;
  }

  public String getName() {
    return name;
  }

  public void setRollNo(int rollNo) {
    this.rollNo = rollNo;
  }

  public void setName(String name) {
    this.name = name;
  }

  @Override
  public String toString() {
    return "Student [rollNo=" + rollNo + ", name=" + name + "]";
  }
}

public class Program {

  public static void main(String[] args) {

    Student student = new Student();

    student.setRollNo(101);
    student.setName("Suraj");

    System.out.println(student);
  }
}

Output: Student[rollNo = 101, name = Suraj]

Default access modifier

If we do not specify any access modifier in front of a variable, method, or a constructor then it is considered as the default access modifier. We can access default members only within a class and within a package. We cannot access them outside of a package.

Visibility: Within a class, Within a package

Default members are also termed as package-private members. As we cannot use them outside of a package.

Default access modifier same package example

package com.getinputs;

public class Student {

  int rollNo;

  String name;

  @Override
  public String toString() {
    return "Student [rollNo=" + rollNo + ", name=" + name + "]";
  }
}

package com.getinputs;

public class Program {

  public static void main(String[] args) {

    Student student = new Student();

    student.rollNo = 101;
    student.name = "Suraj";

    System.out.println(student);
  }
}

Output: Student[rollNo = 101, name = Suraj]

Note* In the above example Student and Program class are in the same package com.getinputs hence the default members of Student class [ rollNo and name ] are accessible in Program class.

Default access modifier different package example

package com.demo;

public class Student {

  int rollNo;

  String name;

  @Override
  public String toString() {
    return "Student [rollNo=" + rollNo + ", name=" + name + "]";
  }
}

package com.getinputs;

import com.demo.Student;

public class Program {

  public static void main(String[] args) {

    Student student = new Student();

    student.rollNo = 101;
    student.name = "Suraj";

    System.out.println(student);
  }
}

Output: Exception in thread "main" java.lang.Error: Unresolved compilation problems: The field Student.rollNo is not visible
The field Student.name is not visible

Note* In the above example Student class is present in com.demo package whereas the Program class is present in com.getinputs package. Now we are using rollNo and name outside of its package and as we have learned above, we cannot access default members outside of its package hence we are getting a compilation issue.

Can we declare a class as Default?

Yes, we can declare a class as default but we cannot access that class outside of its package. If we try to use a default class outside of its package then we get a compilation exception.

package com.demo;

class Student {

  int rollNo;

  String name;

  @Override
  public String toString() {
    return "Student [rollNo=" + rollNo + ", name=" + name + "]";
  }
}

package com.getinputs;

import com.demo.Student;

public class Program {

  public static void main(String[] args) {

    Student student = new Student();

  }
}

Output: Exception in thread "main" java.lang.Error: Unresolved compilation problems: Student cannot be resolved to a type

Note* Student class is having a default access modifier hence we cannot access them outside its package. Still, we are trying to access it in com.getinputs package hence we are getting a compilation exception.

Protected access modifier

We can declare a member as protected using the protected keyword. We can access protected members within a class, within a package, and in the subclass outside of a package.

Visibility: Within a class, Within a package, Outside package by the subclass

Protected access modifier same package example

package com.getinputs;

public class Student {

  protected int rollNo;

  protected String name;

  @Override
  public String toString() {
    return "Student [rollNo=" + rollNo + ", name=" + name + "]";
  }
}

package com.getinputs;

public class Program {

  public static void main(String[] args) {

    Student student = new Student();

    student.rollNo = 101;
    student.name = "Suraj";

    System.out.println(student);

  }
}

Output: Student[rollNo = 101, name = Suraj]

Note* In the above example both the Student and Program classes are in the same package. The member variables of the Student class is having protected as an access modifier hence we can access them in the Program class.

Protected access modifier different Package example

package com.demo;

public class Student {

  protected int rollNo;

  protected String name;

  protected void display() {
    System.out.println("Student [rollNo=" + rollNo + ", name=" + name + "]");
  }
}

package com.getinputs;

import com.demo.Student;

public class Program extends Student {

  public static void main(String[] args) {

    Program program = new Program();

    program.rollNo = 101;
    program.name = "Suraj";

    program.display();
  }
}

Output: Student[rollNo = 101, name = Suraj]

Note* In the above example Student class and Program class are in different packages. The member variables of the Student class is having protected as an access modifier hence they can be only accessed in different packages using inheritance. As the Program class is extending the Student class hence Program class can access the protected members of the Student class.

Public access modifier

We can declare a member as public using the public keyword. We can access public members in any class or in any package. There is no restriction on the accessibility of a public member.

Visibility: Within a class, Within a package, Outside package by the subclass, Outside package

Public access modifier different package example

package com.demo;

public class Student {

  public int rollNo;

  public String name;

  public void display() {
    System.out.println("Student [rollNo=" + rollNo + ", name=" + name + "]");
  }
}

package com.getinputs;

import com.demo.Student;

public class Program {

  public static void main(String[] args) {

    Student student = new Student();

    student.rollNo = 101;
    student.name = "Suraj";

    student.display();
  }
}

Output: Student[rollNo = 101, name = Suraj]

Note* In the above example Student class and Program class are in different packages. The member variables of the Student class is having public as an access modifier hence they can be accessed in the different packages without any restriction.

Access modifier table

access modifier in java

In this article, we have covered access modifiers and the types of access modifiers in java with examples. I hope you found this article interesting and valuable. Please share this article with your friends and help me grow. If you are having any concerns or questions about this article please comment below. If you want to get in touch with me please visit the Contact Me page and send me an email.

1 thought on “Access modifiers in java”

Leave a Comment