A deep understanding of Java protected

Source: Internet
Author: User

When it comes to the access control character protected, even beginners will feel confident that they have no problem understanding in this regard. Well, let's take a look at the question .....

See the code at both ends below. Both cats and rats in Package B inherit the animal class.Copy codeThe Code is as follows:// Code 1: There is an animal in Package
Package testa;
Public class Animal {
Protected void crowl (String c ){
System. out. println (c );
}
}
// Code 2: There are two classes in Package B: CAT and rat.
Package testb;
Import testa. Animal;
Class Cat extends Animal {

}
Class Rat extends Animal {
Public void crowl (){
Crowl ("zhi"); // No problem. It inherits the protected method in Animal-crowl (String)
Cat cat = new Cat ();
Cat. crowl ("miao"); // wrong, The method crowl (String) from the type Animal is not visible
}
}

Since both the cat and the mouse inherit the animal class, the crowl () method inherited by the cat cannot be seen in the scope of the rat's role?

Crux:
The rules for protected access protection are subtle. Although the protected field is visible to all subclasses. But it is very important that the subclass can only access the parent class protected field inherited by itself within its own scope of effect, and cannot access other subclasses (same as the parent class of the parent class) the inherited protected domain. To put it bluntly, the mouse can only be called "zhi, zhi ". Even if he can see a cat (he can create a cat object in his own scope), he will never learn to call a cat.
That is to say, the crowl method inherited by cat is visible within the scope of the cat class. But it is invisible within the scope of the rat class, even if rat and cat are brothers.
In addition:This is why we cannot simply clone () the object aObject directly when using the clone method.

In short, when B extends A, the protected method of the object defined by B can only be called within the scope of the action of subclass B (this method is inherited from parent class ). You cannot call the protected method of other Class A objects.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.