java's permission modifier: public,protected,private

Keywords: Programming Java

Preface

I think the program is rigorous - step by step along a flow chart, then the results will naturally appear - this is a kind of beauty of the program.

As an excellent programming and development language, java should have such beauty in judging a method and variable modified by java modifier.

After learning all kinds of articles on the Internet, I think this process can be expressed as follows:

Next, I will refine some of my expressions in the flowchart.

At the same time, in order to simplify the expression, we will use the word "subject" when referring to what we discussed later, that is, methods / members modified by modifiers.

At the same time, the object decorated by the modifier may be a method or a member variable. This article does not discuss member variables separately, because it is allowed (called) according to the flow chart, which means allowed (read-write) for member variables.

text

0. Inner call and instance call

Inner call: this.method() is the inner call. Obviously, this can be omitted in general. At the same time, the rewriting of methods also belongs to this class. Instance call: the instance variable call of the class to which the subject belongs is the instance call mentioned in the article. If a static method is called directly without an instance, it also belongs to this class. Emphasis: the instance variable here is of the class to which the subject belongs, not of any subclass of the class to which the subject belongs.

1. Call in this class

Any topic (see the last paragraph of the preface for the meaning of the word "topic") is obviously in a class (Interface). This class may have other methods besides what we discussed. In other ways, calling the subject is what I call "intra class invocation".

Code example:

package packetA;

public class ParentA {

    private void privateMethod(){
        System.out.println("privatedMethod");
    }

    public void test(){
        privateMethod();
    }
}

The call to the privateMethod method in the method test, which is called intra class call.

2. Subclass override

That is, through the extends keyword, the class that inherits the subject is the subclass here.

Rewrite: rewrite the implementation process of the method of the parent class. Neither the return value nor the parameter can be changed.

Code example:

package packetA;

public class ChildA extends ParentA {

    @Override
    protected void protectedMethod() {
        System.out.println("childA has overridden the protectedMethod.");
    }
}

3. Call in subclass

Code example:

package packetA;

public class ChildA extends ParentA {
    public void test(){
        this.protectedMethod();
    }
}

4. Internal instance call of this class

Code example:

package packetA;

public class ParentA {

    private void privateMethod(){
        System.out.println("privatedMethod");
    }

    protected void protectedMethod(){
        System.out.println("protectedMethod");
    }

    public void test(){
        ParentA parentA = new ParentA();
        parentA.privateMethod();
    }
}

among

public void test(){
    ParentA parentA = new ParentA();
    parentA.privateMethod();
}

In this way, after instantiating an entity, when the entity calls a method, it is the "..." Instance call ".

5. Instance call under the same package

In the class where the subject is located, the instance call is also the instance call under the same package, but in this case, it will not go to this judgment step, and will go to "yes" when "4. Internal instance call of this class" and reach the allowed end state.

Yes, as long as it's in the same package, or in the subpackage of the package where the class of the subject is located, no matter what kind of class -- is it a subclass of the class of the subject? Classes unrelated to the class of the subject? It doesn't matter - it's OK to call topics that are not modified by the private modifier.

Such as:

package packetA;

public class ParentA {

    private void privateMethod() {
        System.out.println("privatedMethod");
    }

    protected void protectedMethod() {
        System.out.println("protectedMethod");
    }

}
package packetA;

public class DemoA {

    public static void main(String[] args) {
        ParentA parentA = new ParentA();
        parentA.protectedMethod();
    }
}

It can operate normally.

6. All other instance calls

Yes, all other instance calls except for the above situations are rejected as long as they are not topics decorated with public modifiers.

Even the topic (code below) of calling protected decoration within subclass outside the package is also prohibited.

package packetB;

import packetA.ParentA;

public class GreenhatChild extends ParentA {

    public void test(){
        ParentA parentA = new ParentA();
        parentA.protectedMethod();
    }
}

The test method can't be run, whether it's a unit test or a GreenhatChild class in the main function to call the test method.

Posted by azeem123456 on Thu, 28 Nov 2019 03:06:43 -0800