Rights Management in Java

Source: Internet
Author: User

There are four permissions in Java, Private,protected,default,public, not write as default, each with the following table:


Private
Default
Protected
Public
This class can be accessed
Is
Is
Is
Is
This package is not accessible in this class
Whether
Is
Is
Is
Subclasses of non-packages can access
Whether
Whether
Is
Is
Classes not in this package
Whether
Whether
Whether
Is

The permissions of the class members must not be greater than the permissions of the class, if greater than the permissions of the class prevail. One of the following classes:

Class test{public int i=0;    public void Fun () {System.out.println (i); }}

The class member I and fun although the defined permission is public, but because the class's permissions are default, I,fun's permissions are still default.

When accessing a class member through an object, you should also follow the permissions as follows:

Class test{    public static void main (String[] args) {         myclass a=new myclass ();         A.FUN1 ();//error, because FUN1 's permission is private        a.fun2 ();         system.out.println (A.I1);//error because I1 permissions are private         system.out.println (A.I2);     }}class myclass{     private int i1=0;    int i2=0;     PRIVATE VOID FUN1 ()     {         System.out.println ("fun1 ()");      }     void fun2 ()      {         system.out.println ("Fun2 ( ) ");       }} 


This article is from the "11114468" blog, please be sure to keep this source http://11124468.blog.51cto.com/11114468/1735676

Rights Management in Java

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.