Details of the Java protected

Source: Internet
Author: User

1. Java Permissions Control--Most people are brainwashed by mistake.

A major pit, or a major misunderstanding, or a brainwashing of thousands of Java programmers, the wrong concept is:

The public private protected is based on methods and objects.

For example, something private is not accessible to objects, but methods in a class can be accessed.

For example, things that are modified by public, objects, and methods in the class can be accessed.

It's a fraught, and you can take it all as a garbage collection.

2. What is a positive solution?

Let's start with public and private, and take a look at some of the excerpts from an article:

In the JVM, like in Java, every member have an associated access attribute:public, private, protected, or default. The attribute determines the circumstances in which, the member can be accessed. Let M am a member declared in a class C this belongs to a package p. If m are public and it can be accessed by (code in) a NY class. If m is private, it can being accessed only by C. If m has the default access, it can be accessed with the any class that belongs to P.

Well, I'm basically translating:

Every member in Java (Member) has an access control modifier: public,private,protected, or Nothing, is the default. This access control modifier determines the "bad environment where this member can be accessed".

Suppose there is a class C that belongs to a package p,c has a member M.

If M is public, pay attention to the following sentence:"M can be accessed by code in any class." "See in English by (code in)".

If M is private, "m can only be accessed by code in Class C."

By default, the following is also said, if M is the default, is nothing to write, inside the package, the equivalent ofpublic.

OK, put a piece of code, let you know what this saying means:

class c{    privateint  A;      Public void Printa (c c) {        System.out.println (C.A);    }}

In the above code, I have obviously visited the a variable in class C. And it is accessed by C.A in this way, that is, "in the brainwashing point of view [through object access]", according to the "Brainwashing point of view", this sentence is the compilation does not pass.

However, this sentence is obvious, nothing wrong, why. You interpret it as a positive solution:

Where is the environment I visited? In class C, it means that my code appears in Class C, remember the "by code" above? This can of course access the C Class A. "You don't care which object's a, is not related to the specific object" "You do not care which object's a, with the specific object has no relation" "You do not care which object's a, with the specific object is not related" important thing to say three times.

3. A bit more complicated protected.

Or first move the English:

If m is protected, things was slightly more complicated. First, M can is accessed by any class belonging to P, as if it had default access. In addition, it can is accessed by any subclass s of C that belongs to a package different from P, with the following rest Riction:if m is not static and then the class O of the object whose member are being accessed must be s or a subclass of S, W Ritten O≤s (if M is static, the restriction does not apply:m can being always accessed by s). The relationship among C, S, O, M, and P is depicted in Figure 1, where the double-line arrow labeled by + denotes one or More direct superclasses and the double-line arrow labeled By∗denotes zero or more direct superclasses.

Well, I'd better translate it first:

If M is protected, the situation is slightly more complicated.

First, M can be freely accessed by other classes in this package, note that when we say class, you must add a word, by code in, to reverse brainwashing. In other words, M can be accessed by code in any class in this package .

The following is a complicated writing, I simplify the argument, draw a picture:

Suppose P is the package name, K is the package name, that is, not a package.

s inherits from C.

So the code in the S class wants to access M, there is a limit:

o must be a subclass type of s or S.

This is the o≤s in English.

The reason for doing this is to think for yourself ....

Or you can find this article yourself:

Checking Access to Protected members in the Java Virtual machine

Details of the Java protected

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.