Java_se basic--59. Access modifiers

Source: Internet
Author: User
Tags modifiers

Understanding the concept of the package, you can systematically introduce the level of access control in Java. In Java, four access levels are provided for classes, member methods, and properties, respectively private, default, protected, and public.


Permission access modifiers (permissions from large to small right row)
Public (Common) Protected (Protected) Default (Defaults) Private (privately)
The same class
Same package X
Child Parent Class X X
Different packages X X X


Public and private here I do not introduce one by one, presumably everyone than I am familiar with it. and protected and default in the same class I do not verify (because the least privileged private can be in the same class to each other access, both of which are sure to do)


Verify that the properties of the protected adornment can be accessed in the same package. :
Package D;class Demo7 {protected int a = 1;}
Package D;public class Demo8 {public static void main (string[] args) {Demo7 z = new Demo7 (); System.out.println ("a=" +z.a);}
Operation Result:

can access to!

verifies that the default (default) decorated property can be accessed in the same package. :
Package D;class Demo7 {int a = 1;}
Package D;public class Demo8 {public static void main (string[] args) {Demo7 z = new Demo7 (); System.out.println ("a=" +z.a);}
Operation Result:

can access to!

Verify that the properties of the protected adornment can be accessed in the child parent class. :
Package D;class Demo7 {protected int a = 1;}
Package D;public class Demo8 extends Demo7 {public static void main (string[] args) {Demo8 z = new Demo8 (); System.out.println ("a=" +z.a);}
Operation Result:


can access to!

verifies that the default (default) decorated property can be accessed in the child parent class. :
Package D;class Demo7 {int a = 1;}
Package E;import d.*;p ublic class Demo8 extends Demo7 {public static void main (string[] args) {Demo8 z = new Demo8 (); System.out.println ("a=" +z.a);}
operation Result:


cannot access the to!
Verify that the properties of the protected adornment can be accessed in different packages. :
Package D;class Demo7 {protected int a = 1;}
Package E;import d.*;p ublic class demo8{public static void Main (string[] args) {Demo7 z = new Demo7 (); System.out.println ("a=" +z.a);}
Operation Result:

cannot access the to!
verifies that the default (default) decorated property can be accessed in different packages. :
Package D;class Demo7 {int a = 1;}
Package E;import d.*;p ublic class demo8{public static void Main (string[] args) {Demo7 z = new Demo7 (); System.out.println ("a=" +z.a);}
Operation Result:
cannot access the to!

ac Penguin: 654249738, and self-taught Exchange group: 517284938

Java_se basic--59. Access modifiers

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.