Home > Java > Javagetting Started > body text

Detailed example of protected keyword in java

王林
Release: 2020-06-02 16:52:40
forward
2797 people have browsed it

Detailed example of protected keyword in java

First of all, let’s take a look at the following two examples:

Example 1:

//包A中有一个动物类  
package testa;  
public class Animal {  
    protected void crowl(String c){  
        System.out.println(c);  
    }  
}
Copy after login

(Video tutorial recommendation: java Video)

Example 2:

package testb;
import testa.Animal;
 
class Cat extends Animal 
{  
    
}  
public class Rat extends Animal{  
    public void crowl(){  
              this.crowl("zhi zhi"); //没有问题,继承了Animal中的protected方法——crowl(String)  
              Animal ani=new Animal();
              ani.crowl("animail jiaojiao"); //wrong, The method crowl(String) from the type Animal is not visible 
              Cat cat=new Cat();  
              cat.crowl("miao miao"); //wrong, The method crowl(String) from the type Animal is not visible  
    }  
}
Copy after login

Since both cats and mice inherit the animal class, then within the scope of the mouse class, the inheritance of the cat cannot be seen What about the crow() method?

Question Answer:

protected Access protection rules are very subtle. Although the protected domain is visible to all subclasses. But one thing is very important. When is in different packages, the subclass can only access the protected domain of the parent class it inherits within its own scope, but cannot access other subclasses (with the same parent). The protected domain inherited by the class's biological brother) and the protected domain ani.crow1() of the parent class object. To put it bluntly: mice can only call "zhi, zhi". Even if he can see the cat (he can create a cat object in his own scope), he will never learn to meow.

In other words, the crowl method inherited by cat is visible within the scope of the cat class. But it is not visible within the scope of the rat class, even if rat and cat are brothers.

In addition: This is why we cannot simply directly clone the object aObject.clone() when using the clone method. It needs to be in aObject.bObject=(Bobject)this.bObject.clone();

Summary:

When B extends A, within the scope of subclass B, only Call the protected method of the object defined by this subclass B (this method is inherited from the parent class A). The protected methods of other objects of class A (A itself and inherited from A) cannot be called.

Recommended tutorial: java entry program

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

Related labels:
source:csdn.net
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