📜  Java的包与私有访问修饰符

📅  最后修改于: 2021-09-12 10:33:41             🧑  作者: Mango

每当我们编写我们的类时,我们必须向 JVM 提供一些关于我们的类的信息,比如这个类是否可以从任何地方访问,是否可以创建子类,是否可以创建对象等。我们可以通过在Java使用适当的关键字访问修饰符来指定此信息。因此访问修饰符用于设置类、方法和其他成员的可访问性。

让我们分别深入讨论这两个修饰符

  • 包(默认)访问修饰符
  • 私有访问修饰符

修饰符 1:包(默认)访问修饰符

一个没有任何访问修饰符的类或方法或变量声明然后被认为它有一个包(默认)访问修饰符默认修饰符在同一个包内作为公共的,在包外作为私有的。如果一个类被声明为默认,那么我们只能在当前包中访问该类,即从外部包中我们不能访问它。因此,默认访问修饰符也称为包级访问修饰符。类似的规则也适用于Java的变量和方法。

例子:

Java
// Java Program to illustrate Package Level Access Modifier
   
// Importing utility classes
// Importing input output classes
import java.io.*;
import java.util.*;
   
// Main Class
class GFG {
   
    // Declaring default variables that is
    // having no access modifier
    String s = "Geeksfor";
    String s1 = "Geeks";
   
    // Method 1
    // To declare a default method
    String fullName()
    {
        // Concatenation of strings
        return s + s1;
    }
   
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
   
        // Creating an object of main class(GFG)
        // in the main() method
        GFG g = new GFG();
   
        // Calling method1 using class instance
        // and printing the concation of strings
        System.out.println(g.fullName());
    }
}


Java
// Java Program to showcase Private Access Modifier
 
// Import required packages
import java.io.*;
import java.util.*;
 
// Class 1
// Helper class
class A {
 
    // Method
    private void m1()
    {
 
        // Print statement whenever the method is called
        System.out.println("GFG");
    }
}
 
// Class 2
// Main class
class B {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of type class A
        // in main() method
        A a = new A();
 
        // Now accessing the method m1() of
        // class created above
        a.m1();
    }
}


输出:

GeeksforGeeks

修饰符 2:私有访问修饰符

此修饰符不适用于顶级类或接口。它仅适用于类中的构造函数、方法和字段。如果变量或方法或构造函数被声明为私有,因为我们只能从类内部访问它们,即从类外部我们无法访问它们。

例子:

Java

// Java Program to showcase Private Access Modifier
 
// Import required packages
import java.io.*;
import java.util.*;
 
// Class 1
// Helper class
class A {
 
    // Method
    private void m1()
    {
 
        // Print statement whenever the method is called
        System.out.println("GFG");
    }
}
 
// Class 2
// Main class
class B {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of type class A
        // in main() method
        A a = new A();
 
        // Now accessing the method m1() of
        // class created above
        a.m1();
    }
}

输出:

最后,在完成它们之后,让我们总结它们之间的明显差异如下:

 Package Access Modifier Private Access Modifier
This modifier is applicable for both top-level classes and interfaces. This modifier is not applicable for both top-level classes and interfaces.
Package members can be accessed from the child class of the same package. Private members cannot be accessed from the child class of the same package.
Package members can be accessed from a non-child class of the same package. Private members cannot be accessed from a non-child class of the same package.
The package modifier is more accessible than the private modifier. The private modifier is more restricted than a package modifier.
Package modifier provides the lowest level of Encapsulation in java. A private modifier provides a higher level of Encapsulation in Java.