Java protected access permission

Source: Internet
Author: User

In fact, this is simply a saying: "A subclass that is not in the same package as the base class can only access protected members inherited from the base class, you cannot access the protected members of the base-class instance ".

 

 

Original works can be reprinted. During reprinting, you must mark the original source, author information, and this statement in hyperlink form. Otherwise, legal liability will be held. Http://zhangjunhd.blog.51cto.com/113473/19287 This article analyzes protected Access permission.Author: ZJ 2007-3-5blog: [url] http://zhangjunhd.blog.51cto.com/#/url. See example 1: Test. Java
ClassMyobject {}Public ClassTest {Public Static VoidMain (string [] ARGs) {myobject OBJ =NewMyobject (); obj. Clone (); // compile error .}}
The above error occurs: The method clone from the type object is not visiuable. We know that object. Clone () is the protected method. This shows that this method can be accessed by sub-classes of the same package (Java. Lang) and IT (Java. Lang. object. Here is the myobject class (which inherits java. Lang. object by default ). Similarly, test is a subclass of Java. Lang. object. However, you cannot access the protected method of another subclass in one subclass, even though these two subclasses inherit from the same parent class. Let's look at Example 2: test2.java.
ClassMyobject2 {ProtectedObject clone ()ThrowsClonenotsupportedexception {Return Super. Clone ();}}Public ClassTest2 {Public Static VoidMain (string [] ARGs)ThrowsClonenotsupportedexception {myobject2 OBJ =NewMyobject2 (); obj. Clone (); // compile OK .}}
Here, we overwrite (override) the clone () method of the parent class in the myobject2 class, call the clone () method in Test2 of the other class, and compile it. The reason for compilation is obvious. When you override the clone () method in the myobject2 class, the myobject2 class and the Test2 class are in the same package, so the protected method is visible to the Test2 class. After analysis, let's recall the statement in section 2.2 in the shallow copy and deep copy articles in Java, Override the clone () of the base class in the derived class () And declare it as public .Now you understand the reason for this sentence (to enable other classes to call the clone () method of this class, set the attribute of the clone () method to public after the overload ). Next let's take a look at Example 3: test3.java
Package1ClassMyobject3 {ProtectedObject clone ()ThrowsClonenotsupportedexception {Return Super. Clone ();}}Package2Public ClassTest3ExtendsMyobject3 {Public Static VoidMain (string ARGs []) {myobject3 OBJ =NewMyobject3 (); obj. Clone (); // compile error. test3 tobj =NewTest3 (); tobj. Clone (); // complie OK .}}
Here I use the test3 class to inherit from myobject3. Note that these two classes are different packages; otherwise, this is the case of Example 2. Call the clone () method of the Instance tobj of test3 class in test3 class and compile it. Similarly, the clone () method of the Instance OBJ of the myobject3 class is called, and the compilation is incorrect! Unexpectedly, can the protected method be accessed by an inherited class? It must be clear that the class test3 does inherit the class myobject3 (including its clone method), so you can call your own clone method in the class test3. However, the protected method of myobject3 is invisible to test3. For example 1, although myobject and test belong to the same package, the protected clone method comes from Java. lang. object type. In test, the protected methods of its base class objects are invisible.

For example 2, myobject and test are in the same package. The protected clone method comes from myobject, so it is visible to test. In addition, this example shows that the super keyword is an "exception" in the language design for calls to protected members of the base class ".

Example 3 is simple, that is, to call the protected method of the base class instance from the subclass across different packages.

Finally, I am adding two examples:
Example 4: (for example 1)
Class myobject extends test {}
Public class test {
Public static void main (string [] ARGs ){
Myobject OBJ = new myobject ();
OBJ. Clone (); // compile OK.
}
}
Why is it visible? Because the clone method of myobject inherits from test, and test, as a subclass of object, can access its own protected method ~

Example 5:
Package 1
Class myobject extends test {
}

Package 2
Public class test {
Public static void main (string ARGs []) {
Myobject OBJ = new myobject ();
OBJ. Clone (); // compile OK
}
}
Although it is in different packages, the protected method of the subclass actually inherits from the parent class. The protected method of the parent class is visible to itself. The principle is the same as Example 4.

Example 6:
Package 1
Class myobject extends test {
Protected object clone () throws clonenotsupportedexception {
Return super. Clone ();
}
}

Package 2
Public class test {
Public static void main (string ARGs []) {
Myobject OBJ = new myobject ();
OBJ. Clone (); // compile error!
}
}
The protected methods of subclasses in different packages cannot be visible to the parent class ~

Additional Example 7:
Package;
Class myobject extends test {
Public static void main (string [] ARGs ){
Test test = new test ();
Test. Clone (); // compile error.
}
}
Package;
Public class test {

}
In the same package, the clone method of the parent class instance is not visible in the subclass. The principle is the same as Example 1 because the clone method of the parent class is actually from Java. Lang. Object

 

Related Article

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.