Analyze the public, protected, private, and default access permissions of java from the perspective of instances, publicprotected

Source: Internet
Author: User

Analyze the public, protected, private, and default access permissions of java from the perspective of instances, publicprotected

 

1. public

  Same package

1. Internal

Public class A {public int f = 1; public void m1 () {} public void m2 () {f = 2; // class internal normal access m1 (); // class internal normal access }}

 

2. Subclass

package same;public class A {    public int f=1;    public void m1() {}}
Package same; public class B extends A {public void m2 () {f = 2; // call the public member variable m1 () of the parent class (); // call the public Member method of the parent class }}

3. Common classes

package same;public class A {    public int f=1;    public void m1() {}}
Package same; public class B {public static void main (String [] args) {A = new a ();. f = 2; // access A from the public member variable in the image of Class a in the same package. m1 (); // access the public member method in the image of Class A in the same package }}

  Between different packages

  1. Subclass

package same;public class A {    public int f=1;    public void m1() {}}
Package other; import same. a; public class B extends A {public void m2 () {f = 2; // access m1 () for the public member variable of class (); // access the public member method of Class }}

  

2. Common classes

package same;public class A {    public int f=1;    public void m1() {}}
Package other; import same. a; public class B {public static void main (String [] args) {A a = new A ();. f = 2; // different packages access. m1 (); // access the public member method in the image of Class A between different packages }}

Ii. protected

 Same package

1. Internal

Package same; public class A {protected int f = 1; protected void m1 () {} public void m2 () {f = 2; // access the protected member variable m1 () of this class; // access the protected member method of this class }}

2. Subclass

package same;public class A {    protected int f=1;    protected void m1() {}}
Package same; public class B extends A {public void m2 () {f = 2; // access the protected member variable m1 () of the parent class (); // access the protected member method of the parent class }}

3. Common classes

package same;public class A {    protected int f=1;    protected void m1() {}}
Package same; public class B {public void m2 () {A a = new A ();. f = 2; // access the protected member variable of Class. m1 (); // access the protected member method of Class A's object }}

  Between different packages

1. Subclass

package same;public class A {    protected int f=1;    protected void m1() {}}
Package other; import same. a; public class B extends A {public void m2 () {f = 2; // access m1 () for the protected member variable of class (); // access the protected member method of Class }}

2. Common classes

  

package same;public class A {    protected int f=1;    protected void m1() {}}
Package other; import same. a; public class B {public void m2 () {A a = new A (); //. f = 2; Class A cannot access the protected member variable of the image. //. m1 (); the method for the protected member variable of the Class A Object }}

Iii. default

 Same package

1. Internal

Package same; public class A {int f = 1; void m1 () {} public void m2 () {f = 2; // access the default member variable m1 () of this class; // access the default member method of this class }}

2. Subclass

package same;public class A {    int f=1;    void m1() {}}
Package same; public class B extends A {public void m2 () {f = 2; // access the default member variable m1 () of class A objects (); // access the default member method of Class A Object }}

 

3. Common classes

package same;public class A {    int f=1;    void m1() {}}
Package same; public class B {public void m2 () {A a = new A ();. f = 2; // access the default member variable. m1 (); // access the default member method of Class A objects }}

Between different packages

1. Subclass

package same;public class A {    int f=1;    void m1() {}}
Package other; import same. a; public class B extends A {public void m2 () {// f = 2; unable to access the default member variable of class A // m1 (); unable to perform the default member variable method for Class }}

2. Common classes

package same;public class A {    int f=1;    void m1() {}}
Package other; import same. a; public class B {public void m2 () {A a = new A (); //. f = 2; cannot access the default member variable of the Image of Class A. //. m1 (); Method for the default member variable of Class A's object }}

 

Iv. private

  Same package

1. Internal

Package same; public class A {private int f = 1; private void m1 () {} public void m2 () {f = 2; // access the private member variable m1 () of this class; // access the private member method of this class }}

2. Subclass

package same;public class A {    private int f=1;    private void m1() {}}
Package same; public class B extends A {public void m2 () {// f = 2; private member variables of class A cannot be accessed // m1 (); unable to access the private member method of Class }}

  

3. Common classes

package same;public class A {    private int f=1;    private void m1() {}}
Package same; public class B {public void m2 () {A a = new A (); //. f = 2; the private member variable of Class A objects cannot be accessed. //. m1 (); unable to access the private member method of Class A Object }}

Between different packages

1. Subclass

package same;public class A {   private  int f=1;   private  void m1() {}}
Package other; import same. a; public class B extends A {public void m2 () {// f = 2; unable to access the private member variable of class A // m1 (); unable to perform the private member variable method for Class }}

2. Common classes

package same;public class A {   private  int f=1;   private  void m1() {}}
Package other; import same. a; public class B {public void m2 () {A a = new A (); //. f = 2; cannot access the private member variable of the Image of Class A. //. m1 (); Method for private member variable of Class A's object }}

 

Related Article

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.