Reread the differences between public, protected, private and permission without modifier in the series of Java programming ideas

preface

I believe you use more modification permissions for public and private in your daily work. Less is used for protected and no modifiers. I also saw this problem when I was reading, so I summarized and studied it in detail to carefully distinguish these modifiers. Share with you for your reference.

Mapping

Let's take a look at an overall map to get an overall understanding of these access permissions. Then we verify them by examples.

Public modifier

Class interior

We define a class in which variables can be accessed normally.

public class PublicVar {

    public String pubicString;

    public void test() {
        String s = pubicString;
    }

}

Same package

Under the same package of the current class, create a class, create an object, and find the public variable of the class that can be accessed normally.

public class PublicVarSamePackage {

    public void test(){
        PublicVar publicVar = new PublicVar();
        String a = publicVar.pubicString;
    }

}

Subclass

public class ChildrenPublicVar extends PublicVar {

    public void test(){
        String s = pubicString;
    }

}

The public variable of the parent class can be accessed in the child class. It should be noted that subclasses can be in the same package or in different packages.

Other scope

Other ranges can be understood as whether public variables can be accessed in classes of other packages. It's OK here.

public class PublicVarDiffPackage {

    public void test(){
        PublicVar publicVar = new PublicVar();
        String a = publicVar.pubicString;
    }

}

Protected modifier

Class interior

Like the public modifier, it can be accessed normally inside the class.

public class ProtectedVar {

    protected String protectedString;

    public void test() {
        String s = protectedString;
    }

}

Same package

Classes in the same package as the current class can normally access variables.

public class ProtectedVarSamePackage {

    public void test(){
        ProtectedVar protectedVar = new ProtectedVar ();
        String a = protectedVar.protectedString;
    }

}

Subclass

When it is a subclass, the variable can be accessed normally whether the subclass and the parent class are in the same package or different packages.

public class ChildrenProtectedVar extends ProtectedVar {

    public void test(){
        String s = protectedString;
    }

}

Other scope

In other ranges, if it is in the same package as the current class, you can access the protected variable. If you are not in the same package, you cannot access it, and the compiler will report an error.

Private modifier

Class interior

Private modified variables can be accessed normally inside the class.

Same package

Private variables cannot be accessed by other classes under the same package.

Subclass

Subclasses also cannot access private variables.

Other scope

Other scopes cannot access the private variable of the class.

No modifier

The no modifier and the protected modifier are inside the class, the same as the package and other access modes, but they are different for subclasses.

Class interior

It can be accessed normally.

Same package

It can be accessed normally.

Other scope

It can be within the normal range.

Subclass

Subclasses. When the subclass and the parent class are in the same package, the subclass can access the non modifier variables of the parent class. When a subclass has a different package from the parent class, the subclass cannot access the non modifier variable of the parent class. The compiler will prompt an error.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>