Java basics: keyword protected

Source: Internet
Author: User

During development, the keyword protected is frequently encountered. In particular, in some callback methods, let's make a summary today.

 

The following describes the relationship between access permission modifiers in Java, as shown in:

 

Perform several tests to illustrate the protected keyword.

 

1. In different classes of the same package, you can access the protected member without any inheritance relationship.

Package mark. test; <br/> public class person {<br/> protected int height; </P> <p> protected void speak (Object OBJ) {<br/> system. out. println ("I'm" + OBJ + "! "); <Br/>}< br/>/** <br/> * in different classes of the same package, can access the protected member <br/> * No inheritance relationship <br/> */<br/> class man {</P> <p> Public static void main (string [] ARGs) {<br/> person P = new person (); <br/> man = new man (); <br/> P. height = 10; <br/> P. speak (man); <br/>}</P> <p> @ override <br/> Public String tostring () {<br/> return "man "; <br/>}< br/>} 

2. In different classes of the same package, you can access the protected member, which has an inheritance relationship.

Package mark. test; <br/> public class person {<br/> protected int height; </P> <p> protected void speak (Object OBJ) {<br/> system. out. println ("I'm" + OBJ + "! "); <Br/>}< br/>/** <br/> * in different classes of the same package, you can access the protected member <br/> * with an inheritance relationship <br/> */<br/> class woman extends person {</P> <p> Public static void main (string [] ARGs) {<br/> woman W = New Woman (); <br/> W. speak (w); <br/>}</P> <p> @ override <br/> Public String tostring () {<br/> return "woman "; <br/>}< br/>} 

3. In the same package, the indirect subclass can access the protected member of the parent class.

class Wife extends Woman {void tt() {Wife wife = new Wife();wife.speak(wife);Woman woman = new Woman();woman.speak(woman);Person p = new Person();p.height = 20;p.speak(man);}

4. Inheritance and non-inheritance for different packages

In different packages, this parent class object cannot access its own protected member even if it is a subclass with an inheritance relationship.

Package mark. zhang; <br/> Import mark. test. person; <br/>/** <br/> * The classes under different packages cannot access the protected members of the parent class <br/> * do not have an inheritance relationship <br /> */<br/> public class man {</P> <p> Public static void main (string [] ARGs) {<br/> person P = new person (); <br/> // P. height = 20; // inaccessible <br/> // P. speak (man); // inaccessible <br/>}</P> <p> @ override <br/> Public String tostring () {<br/> return "man "; <br/>}< br/>/** <br/> * subclasses of different packages can access the protected members of the parent class. <br/> * <br /> * has an inheritance relationship <br/> */<br/> class woman extends person {</P> <p> Public static void main (string [] ARGs) {<br/> woman W = New Woman (); <br/> W. height = 100; <br/> W. speak (w); <br/> person P = new person (); <br/> // P. height = 20; // inaccessible <br/> // P. speak (man); // inaccessible <br/>}</P> <p> @ override <br/> Public String tostring () {<br/> return "woman"; <br/>}< br/>} 

5. Indirect subclasses of different packages

The protected method of the parent class is invisible to indirect subclass under different packages, that is, it cannot be accessed!

Package mark. zhang; <br/> public class person {<br/> protected int height; </P> <p> protected void speak (Object OBJ) {<br/> system. out. println ("I'm" + OBJ + "! "); <Br/>}< br/> package mark. zhang; <br/>/** <br/> * in different classes of the same package, you can access the protected member <br/> * with an inheritance relationship <br/> */<br/> public class woman extends person {</P> <p> Public static void main (string [] ARGs) {<br/> woman W = New Woman (); <br/> W. speak (w); <br/>}</P> <p> @ override <br/> Public String tostring () {<br/> return "woman "; <br/>}< br/> package mark. test; <br/> Import mark. zhang. person; <br/> Import mark. zhang. woman; <br/>/** <br/> * This subclass wife cannot access the protected member of the parent class person. <br/> * wife is an indirect subclass of person. <br/> */<br/> class wife extends woman {<br/> Public static void main (string [] ARGs) {<br/> wife = new wife (); <br/> wife. speak (wife); // error <br/> woman = New Woman (); <br/> // woman. speak (woman); // error, the method speak (object) from the type person is not visible <br/> person P = new person (); <br/> // P. height = 20; // inaccessible <br/> // P. speak (woman); // inaccessible <br/>}</P> <p> @ override <br/> Public String tostring () {<br/> return "wife"; <br/>}< br/>} 

OK. The test is over !!!

 

Summary:

 

<1> users of the same package can freely access the protected member.

<2> different classes in the same package do not have any inheritance relationships, so you can freely access protected members.

<3> different classes in the same package have an inheritance relationship. Any subclass or subclass can access the protected member.

<4> different packages and different classes do not have an inheritance relationship and cannot access protected members of other classes.

<5> different packages and different classes have inheritance relationships. Child classes can access the protected members of the parent class.

However, the parent class object cannot access its protected member; otherwise, the following error is reported: The method speak (object) from the type person is not visible.

<6> different packages and different classes have inheritance relationships. In the indirect subclass, apart from the class object itself, its direct parent class, and indirect parent classes cannot access their own protected members. In fact, this is the same as <5>.

 

 

 

 

 

 

 

 

 

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.