Protected access Modifiers in Java

Source: Internet
Author: User

The protected methods and properties defined in a class are the same as the default permission methods and properties. For example, a class of protected methods and properties outside the package is not accessible through the class instance (can you access the default permissions of a class outside of the package method and properties?). Of course not), which is why you cannot invoke its protected methods and properties anywhere outside of the package where the object is located, even if it is not in the subclass of the class. What you can "see" in a subclass outside of the class package is just the protected method and property inherited by the subclass, which is the protected method and property of the parent class object that cannot "see" it.

1PackageA
2Public ClassA
3{
4 Protected IntA
5}
6
7Packageb
8ImportA.A;
9Public ClassBExtendsA
10{
11 Public Static voidMain (String args[])
12{
13C C= NewB ();
14B.a= 1;// This sentence is compiled, and can be executed correctly
15 a = new A ();
16 a.a = 1// This sentence is not compiled
Span style= "color: #008080;" >17
18 }

Protected access modifiers in Java

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.