Detailed access control modifiers in Java (public, protected, default, private)

Source: Internet
Author: User
Tags modifiers

The access control modifiers in Java have been confusing to the author for a long time, and the more complicated situations have not been understood thoroughly. Today we are determined to systematically and comprehensively study all aspects of access control modifiers in Java and organize them into this article, hoping that readers with the same doubts will be able to reap the benefits after reading them. If there is an error in the article, please comment that the common communication ~

In front: This article only studies the variables/methods of the access control modifiers in Java that declare classes.

First throw the conclusion:

* Access rights for member variables/methods

* Private Default protected public

* Pack your own class √√√√

* Pack your own other class √√√

* Other package classes have inheritance Yes (subclass) Yes

* Other packages have no inheritance relationship with other classes Yes

PS: There is an inheritance relationship that indicates that the class in which the object is accessed is the parent class.

1. Let's take a look at the definition of access control modifiers in Java.

In Java, you can use access controls to protect access to classes, variables, methods, and construction methods.

The following four forms of access are available:

· member variables of a class access the member variables of a class

· member variables of a class access a member method of a class

· Member methods of a class access member variables of a class

· A member method of a class accesses a member method of a class

PS: The following code is in the third form as an example, the other forms are basically the same.

Depending on the object being accessed, access can be divided into two main categories:

· The Access object is in the same class, which can be accessed directly by the [member variable/method name].

class a {    int a = ten;     void PrintA () {        System.out.println (a);    }}

PrintA () to access a because they are in the same class, they can be accessed directly through a.

· Access to objects in different classes (assuming access to objects in Class B) can be accessed by declaring, initializing an object of B, by the name of the [object name. Member variable/method].

PS: This situation is limited to member methods that access member variables/methods.

class A {    void  printb () {        new  B ();        System.out.println (ob.b);    }} class B {    int b = ten;}

The PRINTB () in a has access to B in B because they are not in the same class, so an object OB of B can be declared, initialized in Printb (), accessed through ob.b.

In addition, when the Access object is a static variable/method, it can be accessed by accessing the class name of the class in which the object resides. The name of the member variable/method.

class a {    staticint a = ten;     int Doublea = a.a * 2;     void Printb () {        System.out.println (b.b);    }} class B {    staticint b = ten;}

Doublea to access A, because a is a static variable, it can be accessed through A.A.

The PRINTB () in a has access to B in B, and because B is a static variable, it can be accessed through b.b.

2. The conclusion mentions the package, and let's look at the definition and function of the package in Java.

To better organize classes, Java provides a package mechanism for distinguishing the namespace of a class name.

The role of the package

  • 1 The functions of similar or related classes or interfaces organized in the same package, convenient for the search and use of the class.
  • 2 As with folders, packages are stored in a tree-like directory. The class names in the same package are different, and the names of the classes in different packages can be the same, and the package name should be distinguished when calling two classes of the same class name in different packages. Therefore, packages can avoid name collisions.
  • 3 packages also restrict access, and classes that have access to the package can access classes in a package.

Java uses the package as a mechanism to prevent naming conflicts, access control, to provide search and locate classes (class), interfaces, enumerations (enumerations), and annotations (annotation), and so on.

For the use of the package, please refer to the Java Tutorial package, which is not detailed here.

It is important to note that the Import keyword introduces a class file, not a Java file.

3. Inheritance is also mentioned in the conclusion, so let's take a look at the definition of inheritance in Java.

Inheritance is a cornerstone of Java object-oriented programming technology because it allows classes of hierarchical hierarchies to be created. Inheritance can be understood as the process by which an object obtains a property from another object.

For details on inheritance, refer to the Java Tutorial inheritance, not detailed here.

It is to be understood that when a subclass inherits the member variable/method of the parent class, it accesses the re-inheritance first. So the rules for access rights above apply to inheritance as well.

In the same package, the member variable/method can be inherited if a member variable/method of the parent class can be accessed. That is, if an object of the parent class is declared or initialized in a subclass member method, you can access a by using [object name. Member variable/method A] After declaring and initializing an object of the subclass, you can also access a through [object name. Member variable/method a].

class extends B {  void  printb () {    new  B ();    System.out.println (ob.b);     New A ();    System.out.println (ob2.b);  }} class B {    int b = ten;}

A inherits B, so a inherits the member variable B of B. Because a in PRINTB (), after declaring, initializing an object ob of B, can access B through ob.b, the Declaration, initialization of an object of a ob2, may be accessed through ob2.b B. (You can inherit if you have access).

However, in different packages, when the subclass inherits the parent class, the child class can only access the public member variable/method of the parent class, but can inherit the protected and public member variables/methods of the parent class. (see the example below)

It is worth noting that the subclass inherits the member variables/methods of the parent class, and does not imply that these member variables/methods exist in the subclass and therefore cannot be accessed directly through [the name of the member variable/method]. A member variable/method that can be understood to be inherited has entered the subclass's XOR (Fog).

Of course, if the inherited member variables/methods are overridden, the member variables/methods are present in the subclass and can be accessed directly by the [member variable/method name].

If you do not discuss polymorphism here, please refer to the Java Tutorial polymorphism.

Back to the conclusion, let's layer through the access control modifiers in Java.

/*Stark.java*/ Packagewinter.is.coming; Public classStark {Private BooleanNed; BooleanRobb; protected BooleanSansa;  Public BooleanArya; voidhowisned () {System.out.println (NED); }}classSnow {voidWhosebastard () {Stark Stark=NewStark (); //System.out.println (stark.ned); not accessibleSystem.out.println (Stark.robb); }}/*Greyjoy.java*/ImportWinter.is.coming.Stark; Public classGreyjoyextendsStark {voidbetray () {Stark Stark=NewStark (); //System.out.println (Stark.robb); not accessible//System.out.println (STARK.SANSA); not accessibleGreyjoy Greyjoy=NewGreyjoy (); //System.out.println (Greyjoy.robb); not accessibleSystem.out.println (GREYJOY.SANSA); }}/*Bolton.java*/ImportWinter.is.coming.Stark; Public classBolton {voidFlay () {Stark Stark=NewStark ();    System.out.println (Stark.arya); }}

① own class-private accessible

Howisned () in stark can access the private type of Ned in stark.

② own package Other class--default accessible

Snow's Whosebastard () can access the default type of Robb in stark, and it is not possible to access the private type of Ned in stark.

③ Other classes have inheritance relationships-protected inheritable

Betray () in Greyjoy can inherit the Sansa of protected type in stark, not stark protected in Sansa, nor can inherit and access the stark of the default type in the Robb.

④ Other Classes No inheritance relationship-public accessible

The Flay () in Bolton can access the Arya of the public type in stark.

Detailed access control modifiers in Java (public, protected, default, private)

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.