Các loại access modifier trong Java (public, protected, default, private)

Các loại access modifier trong Java (public, protected, default, private)

Access modifier là gì?

Access Modifier là các từ dùng trước các khai báo class, biến, method để thể hiện khả năng truy cập các thể hiện của class, biến và method đó ở các class khác.

Với class ta có 2 loại access modifier là public và default nhưng với biến và phương thức thì ta có 4 access modifier (public, protected, default, private)

(Xem lại: Access modifier với class)

Khi nói đến khả năng truy cập (access) ta cần phân biệt 2 trường hợp:

  • Trường hợp 1: khi một method của một class có thể truy cập biến hoặc method của class khác.
  • Trường hợp 2: khi một class con có thể thừa kế các biến/method của class cha.

Ví dụ trường hợp 1:

public class Person {
  public void hello() {
    System.out.println("hello");
  }
}

public class Student {
  public void demo() {
    Person person = new Person();
    // method hello() của class person là public nên ta có thể truy cập nó từ class khác
    person.hello();
  }
}

Ví dụ trường hợp 2:

public class Person {
  public void hello() {
    System.out.println("hello");
  }
}


public class Student extends Person{
  public void demo() {
    // method hello của class Person là public nên các class con của nó có thể kế thừa
    this.hello();
    
    Person person = new Person();
      // method hello() của class person là public nên ta có thể truy cập nó từ class khác
      person.hello();
  }
}

Public modifier (Các thành phần public)

Khi một phương thức hoặc biến được khai báo là public, có nghĩa là tất cả các class khác, kể cả các class không thuộc cùng package đều có thể truy cập

(giả sử các class khác có thể nhìn thấy: ví dụ class A khai báo là default, class B khác package với class A nên không thể nhìn thấy A thì tất cả các biến và phương thức của A sẽ không thể truy cập từ B)

package stackjava.com.demo.package1;
public class A {
  public String str = "abc";

  public void hello() {
    System.out.println("hello");
  }
}


package stackjava.com.demo.package2; // không cùng package với class A
import stackjava.com.demo.package1.A;

public class B {
  public static void main(String[] args) {
    A a = new A();
    // biến str là public nên có thể truy cập từ class khác package
    System.out.println(a.str);
    // phương thức hello là public nên có thể truy cập từ class khác package
    a.hello();

  }
}

Private modifier

Khi một phương thức hoặc biến được khai báo là private nó sẽ không thể truy cập từ class khác, kể cả các class cùng source file hay các class con.

Inner class có thể truy cập được thành phần private của class chứa nó.

Ví dụ:

package stackjava.com.demo.package1;
public class A {
  private String str = "abc";

  class B {
    public void demo() {
      A a = new A();
      System.out.println(a.str);
    }
  }
}

class C {
  public void demo() {
    A a = new A();
    System.out.println(a.str); // compiler error
  }
}

Default modifier

Khi một phương thức hoặc biến được khai báo là default thì chỉ có các class thuộc cùng package với nó mới có thể truy cập.

package stackjava.com.demo.package1;
public class A {
  // default access
  String str = "abc";
}


package stackjava.com.demo.package2; // không cùng package với class A
import stackjava.com.demo.package1.A;

public class B {
  public void demo() {
    A a = new A();
    System.out.println(a.str); // compile error
  }
}

Protected modifier

Protected modifier khá giống với default modifier, nó hạn chế khả năng truy cập trong cùng 1 package, tuy nhiên với protected modifier thì nó còn cho phép truy cập từ các class con kể cả khi class con không nằm cùng package với class cha. (truy cập theo trường hợp thừa kế)

package stackjava.com.demo.package1;
public class A {
  protected String str = "abc";
}

package stackjava.com.demo.package2; // không cùng package với class A
import stackjava.com.demo.package1.A;

public class B extends A{
  public void demo() {
    System.out.println(this.str); // compile success
    A a = new A();
    System.out.println(a.str); // compile error
  }
}

Access modifier với biến cục bộ (Local variable)

Không thể khai báo các biến cục bộ với access modifier. Các biến cục bộ chỉ có thể truy cập trong cùng method chứa nó.

public class A {
  public void demo() {
    String str1; // ok
    
    public String str2; // compile error
    protected String str3; // compile error
    private String str4; // compile error
    
  }
}

Các loại access modifier trong Java (public, protected, default, private)

Các loại access modifier trong Java (public, protected, default, private) stackjava.com

Xem thêm: https://stackjava.com/java-core

References:

SCJP pdf

stackjava.com