Java Basics (vii) Java four access rights

Source: Internet
Author: User
Tags modifiers

Introduction

?? Access in Java is not difficult to understand, but full mastery is not easy, especially 4 access rights are not available at all times. Here's a look at the circumstances under which access permissions are allowed to be selected.

Introduction to access rights

access control: refers to the visibility of other classes in this class and within members of this class (member variables, member methods, inner classes), that is, whether the content allows other classes to be accessed.

In Java, there are four kinds of access control, the size of their permissions control is the case: public > Protected > Default (Package Access) > Private, the specific permissions control look at the table below, The column specifies whether the class has permissions to allow access to the contents of the row under permission control:

access Rights This class class for this package sub-class Outsourcing classes for non-subclasses
Public Is Is Is Is
Protected Is Is Is Whether
Default Is Is Whether Whether
Private Is Whether Whether Whether
Access rights-controlled usage scenarios

The scenarios used for access can be summarized in the following four scenarios, each with different restrictions on the use of access rights:

1. Access control for external classes

An external class (external interface) is relative to an inner class (also known as a nested Class) and an internal interface. Access control for external classes can only be in these two types: public,default .

//public 访问呢权限的外部类,所有类都可以使用这个类publicclass OuterClass {}//default 权限的外部接口,所有类、接口均可以使用此接口interface OuterInterface{    }
2. Access control for members inside a class

The members of a class are divided into three categories: member variables , member Methods , members inner classes (internal interfaces)

The access control for members within a class can be four, which means that all access control permissions can be used

publicclass OuterClass {       publicint//可以被所有的类访问    protectedboolean//可以被所有子类以及本包的类使用        voidcc//default 访问权限,能在本包范围内使用        System.out.println("包访问权限");    }        //private权限的内部类,即这是私有的内部类,只能在本类使用    privateclass InnerClass{            }}

Attention:
The members of the class here refer to the global members of the class and do not include local members (local variables, local inner classes, no local internal interfaces). In other words, local members are not controlled by access rights because local members only work within the scope in which they reside, and cannot be accessed by other classes.

  publicvoidcount(){    //局部成员变量    publicint amount;//编译无法通过,不能用public修饰    int money;//编译通过    //局部嵌套接口    class customer{//编译通过            }}

The two scenarios above can be adapted to almost all situations, but some are special and require some additional access.

3. Access permissions for abstract methods

The common method is that you can use four access rights, but the abstract method has a limitation: it cannot be decorated with private, that is, the abstract method cannot be private, otherwise the subclass cannot inherit the implementation abstract method.

4. Access rights for Interface members

interface because of its particularity, all members of the access rights are defined, the following is the access rights of the interface members:

    • variable: public static final
    • Method: Public abstract
    • internal class, internal interface: public static

And because everything is mandatory by default, so when we use it, we don't necessarily have to write out all the modifiers, which the compiler will do for us, that is, we can write less modifiers, but not the wrong modifier.

publicinterface Interface_Test {        publicint6//少写了  static final     int5//        //嵌套接口,可以不写public static     interface cc{            }}

Java Basics (vii) Java four access rights

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.