Access Modifiers in Java

Introduction to Access Modifiers in Java

Access modifiers in Java are keywords that are used to control the access to classes, methods, fields, and constructors. There are four access modifiers in Java:

  • Public: Public members are accessible from anywhere in the program.
  • Protected: Protected members are accessible from within the class in which they are defined, from subclasses of that class, and from classes in the same package.
  • Default: Default members are accessible from within the class in which they are defined and from classes in the same package.
  • Private: Private members are only accessible from within the class in which they are defined.

The choice of access modifier for a class member depends on the intended use of the member. For example, public members should be used for members that are intended to be used by other classes, while private members should be used for members that are only intended to be used by the class in which they are defined.

Here is an example of how to use access modifiers in Java:

public class MyClass {
  private int myPrivateField;

  protected void myProtectedMethod() {
    // ...
  }

  public void myPublicMethod() {
    // ...
  }
}

In this example, the `myPrivateField` field is private, so it is only accessible from within the `MyClass` class. The `myProtectedMethod()` method is protected, so it is accessible from within the `MyClass` class, from subclasses of the `MyClass` class, and from classes in the same package. The `myPublicMethod()` method is public, so it is accessible from anywhere in the program.

Access modifiers can be used to improve the encapsulation and readability of your code. By using access modifiers wisely, you can make your code more secure and easier to maintain.

Here are some of the benefits of using access modifiers in Java:

  • Improved encapsulation: By using access modifiers, you can control which parts of your code are accessible to other classes. This can help to improve the encapsulation of your code and make it more secure.
  • Increased readability: Access modifiers can make your code more readable and easier to understand. For example, if you see a public method, you know that the method is intended to be used by other classes.
  • Reduced errors: Access modifiers can help to reduce errors in your code. For example, if you try to access a private member from another class, the compiler will give you an error.

Overall, access modifiers are a powerful tool that can help you to write more robust and efficient Java code.

Exercises

Demo Code for Access Modifiers in Java


  //Create the packages and change the access modifiers of the name and address
  //fields in the vitUniversity package to understand its behaviour.
  //vitUniveristy Package Classes
  package vitUniversity;
  public class Person {
  
  protected String name;
  protected String address;
  
  public void test_publicaccess()
  {
  System.out.println(this.name+this.address); //can access it
  }
  }
  
  class assistantProfessor extends Person
  {
  public void test_access()
  {
  System.out.println(this.name+this.address); //yes i can access
  }
  }
  
  class faculty
  {
  public void test_access()
  {
  Person p = new Person();
  System.out.println(p.name+p.address);
  }
  }
  
  //scope is a subpackage under vitUniversity Package. ScopeFaculty is a
  //class under scope package.
  
  package vitUniversity.scope;
  import vitUniversity.Person;
  public class ScopeFaculty {
  
  public void test_access()
  {
  Person p = new Person();
  System.out.println(p.name+p.address);
  }
  }
  
  //This is the code for the default package with the main method.
  //This package contains the democlass and the employee class.
  import vitUniversity.Person;
  public class demoClass {
  
  public static void main(String[] args) {
  // TODO Auto-generated method stub
  Person p = new Person();
  System.out.println(p.name+p.address);
  
  }
  
  }
  
  class employee extends Person
  {
  public void testaccess()
  {
  System.out.println(this.name+this.address);
  }
  }