Java class Access Permissions

Source: Internet
Author: User
Document directory
  • Accesscontrol. accesscontroldemo
  • Accesscontrol. Base
  • Test. accesscontroldemo
  • Test. Bulk
1. Resolution

Java has four types of access permissions, three of which have access permission modifiers: private, public, and protected, and one without any modifiers.

  1. PRIVATE: the shortest modifier for access permission restriction in Java, which is generally called "private ". The classes, attributes, and Methods Modified by the class can only be accessed by the objects of the class. The subclass of the class cannot be accessed, and cross-package access is not allowed.
  2. Default: no access modifier is added, which is usually called "default access mode". In this mode, only access in the same package is allowed.
  3. Protect: an access modifier between public and private. It is generally called a "protected shape ". Classes, attributes, and Methods Modified by the class can only be accessed by methods and subclasses of the class, even if the subclass can be accessed in different packages.
  4. Public: the widest modifier in the Java language. It is generally called "public ". Classes, attributes, and Methods Modified by the package can be accessed not only across classes, but also across packages.
The following table shows the similarities and differences between the four access permissions. The table is as follows:
 

Same class

Same package

Subclass of different packages

Non-subclass of different packages

Private

√

     

Default

√

√

   

Protected

√

√

√

 

Public

√

√

√

√

2. Protected

Assume that there are accesscontroldemo and base classes under the accesscontrol package. Protected double price is a member variable of the base class, because the two classes are in the same package, therefore, you can directly access system in the accesscontroldemo class. out. println (base. price); the specific instance is as follows:

Accesscontrol. accesscontroldemoview code

package accesscontrol;public class AccessControlDemo {    public static void main(String[] args) {        // TODO Auto-generated method stub        Base base=new Base("123-1",120.1);        System.out.println(base.price);            }}
Accesscontrol. baseview code

Package accesscontrol; public class base {private string ISBN; protected double price; // default constructor public base () {}// constructor, if you only define constructors with parameters, instead of Default constructors, the base subclass must define an explicit constructor // implicit super constructor base () is undefined for default constructor. must define an explicit public base (string ISBN, double price) {This. ISBN = ISBN; this. price = price;} Public String getisbn () {return ISBN;} public void setisbn (string ISBN) {This. ISBN = ISBN;} public double getprice () {return price;} public void setprice (double price) {This. price = Price ;}}

However, if we move the accesscontroldemo class to the test package, we will find an error in eclipse and the compilation fails because the member variables of the protected type are invisible in the test package.

Suppose we create a base class subclass bulk in the test package, that is, bulk is a base class.Subclass of different packages. In the bulk class, you can directly access the protected double price. This base-level self-base class member variable is shown in the following example:

Test. accesscontroldemoview code

package test;public class AccessControlDemo {    public static void main(String[] args) {        Bulk bulk=new Bulk("123-1",120.1);        bulk.print();    }}
Test. bulkview code

package test;import accesscontrol.Base;public class Bulk extends Base {        public Bulk() {        super();    }    public Bulk(String isbn, double price) {        super(isbn, price);    }    public void print()    {        System.out.println(this.price);    }}

 

 

 

 

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.