Home > Java > javaTutorial > body text

What is protect in java

(*-*)浩
Release: 2019-05-27 17:56:27
Original
9010 people have browsed it

protect is the permission keyword in java, which generally specifies the scope of use.

What is protect in java

protected:

The protected-modified class members of the parent class are visible in the package and are Its subclasses are visible.

The parent class and the subclass are not in the same package. The subclass can only access the protected members inherited from the parent class, but cannot access the members instantiated by the parent class.

We can further understand the protected keyword through the following examples of visibility of protected methods. When encountering a call involving a protected member, you must first determine where the protected member comes from and what its visibility range is, and then you can determine whether the current usage is feasible

Example:

//示例一
package p1;
public class Father1 {
   protected void f() {}    // 父类Father1中的protected方法
}

package p1;
public class Son1 extends Father1 {}

package p11;
public class Son11 extends Father1{}

package p1;
public class Test1 {
   public static void main(String[] args) {
       Son1 son1 = new Son1();
       son1.f(); // Compile OK     ----(1)
       son1.clone(); // Compile Error     ----(2)

       Son11 son = new Son11();    
       son11.f(); // Compile OK     ----(3)
       son11.clone(); // Compile Error     ----(4)
   }
}
Copy after login

For the above example, first look at (1)(3), where the f() method is inherited from class Father1, and its visibility is package p1 and its subclasses Son1 and Son11, while Since the package of the class Test1 that calls the f() method is also p1, (1) and (3) are compiled successfully. Secondly, look at (2)(4). The visibility of the clone() method is the java.lang package and all its subclasses. For the statements "son1.clone();" and "son11.clone();", two The clone() of the user is visible in classes Son1 and Son11, but is invisible to Test1, so the compilation in (1) and (3) fails.

Summary

protected is the most difficult to understand Java class member access permission modifier. In programming, when encountering a call involving protected, you must first determine where the protected member comes from and what its visibility range is, and then use it correctly.

The above is the detailed content of What is protect in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!