Questions about packages and access modifiers in Java

1. Package

Usually after we create the project (that is, create a module), we then write code directly in the src directory. If written this way, it will be easier to find various types of code we wrote at different times in the future.

1. My project is under the Code file.

2. In Code, there are code modules that I create every day (this makes it easier to find previously written code).

3. So how to create a module? Create a new Module in the Code directory, as shown below.

4. After entering, first select the jdk version of your compiler (I chose the jdk1.8 version here, of course you can also Choose something else, but try not to choose a version that is too high for fear of instability).

5. Then select next below to proceed to the next step, and then name the module you created today (my recommended habit is: for example, if today is November 6th, you can write it as “day_1106”), Finally, just click Finish below to complete.

This is how to create a new module, and then there will be a src directory under the new module. In fact, it is a folder used to record the code you write.

(1) Click the left mouse button, and the src directory will appear.

s

(2) Then right-click in the src directory, the new keyword will appear, and then select Java Class.

(3) Click in and there will be classes, interfaces, etc. that can be created.

6. But in the future, when we do projects in the company or other places, we need to divide each area more clearly. At this time, we have to use a concept of this package. Package it actually has the same function as our folder.

(1) First create a package in the src directory and name it (the general naming rules for package names will be discussed later)

give it a name

Create a class under this package:

At this time, you can find the directories one by one in the newly created module (such as my day_1106) in the project you created (such as my Code)< strong>, until the (Test) class created. (This explains why packages are similar to folders)

(2) Here we can see that the package we created (com.baidu.test) is a third-level directory. The test file is under the baidu file, and the baidu folder is under the com file.

7. The role of packages: In fact, the role is equivalent to files, managing source code resources and accurately performing post-maintenance of the code.

8. The general way to write package names: Inverted company domain name + functional name . What is an inversion of a company domain name?

(1) For example, when we carry out development projects in Baidu Company (that is, www.baidu.com), we will change it to com.baidu.Note that we We don’t know how to write www in it. Later we willwrite this functional name according to the development projectand write it as:com.baidu.XX.XXX .XX…Although sometimes the finer the division, the better, but it is not good to divide it too finely (for example, if there is only one thing in the next file, it is not necessary. ).

(2)For example:com.baidu.bean
com.baidu.dao
com.baidu.service (the function is more service-oriented)

com.feisi.test (the function is more focused on writing test classes)
…and many other ways of writing

(3) At this time we can create a new package under com.baidu.

(4) Two different files will be generated in this secondary directory: test and demo files, and then codes with different functions can be created in different files respectively. At this time, resource management of the source code is achieved. Purpose.

(5) After talking about packages, we will probably talk about Java’s access modifiers. (Because access modifiers involve package knowledge).

2. Access modifiers

1. public (public).

2. protected.

3. default (friendly).

4. private (private).

Table of different permissions for access modifiers in various places
Access modifiers

This category

Same package

(not a subclass)

Subclasses (different packages) Others (different packages are not subclasses)
public OK OK OK OK
protected OK OK OK NO
default OK OK NO NO
private OK NO NO NO

(I will explain why this is the case later)

Tongbao can be simply understood as being of the same clan and clan (people in a family do not have to be related by blood, etc. or have no blood relationship in other ways…).

Example:

1. Just use the directory under the package created above: Create a parent class Person in the demo directory

2. Create a subclass Teacher in another test directory to inherit Person

(Note at this time: When using different package resources and the current class is not in the same package, you must import the package . If it is in the same package, there is no need to import the package: That is to say, if the parent class Person and the subclass Teacher are in different packages, you must use the import keyword, which means import. Format: import + package name + class name ). And note that the import should be placed below the declaration of the package (that is, write the import package import below the package).

The code is shown below:

package com.baidu.test;//This package

import com.baidu.demo.Person; //Introduction package

public class Teacher extends Person {

}

If you find it troublesome, you can directly write: import com.baidu.demo.*; (Point asteriskThis is called importing all classes in this package at once) , just imported a class Person.

3. Now let’s analyze and explain each access permission separately:

1. public:

(1)This category: (completely OK)

(2) The same package is not a subclass: (completely OK)

Then I will create another class in the same package and call it Test (also in the demo directory, called the test class), and exclude non-subclasses.

(3) Different packages but subclasses: (completely OK)

Already created above:

(4) Different packages and not subclasses: (completely OK)

I created a Test class in the test directory, which is not in the same package as the Person class (in the demo directory)

It can be seen that public has the highest access rights and anyone can use it.

2. protected:

(1)This category: (completely OK)

(2) The same package is not a subclass: (completely OK)

(3) Different packages but subclasses: (completely OK)

(4) Different packages and not subclasses: (NO, NO…)

3. default (default, that is, it is the default access modifier if you do not write it):

(1)This category: (completely OK)

(2) The same package is not a subclass: (completely OK)

(3) Different packages but subclasses: (NO, NO…)

(4) Different packages and not subclasses: (NO, NO…)

4. private:

(1)This category: (completely OK)(2) The same package is not a subclass: (NO, NO…)(3) Different packages but subclasses (NO, NO…)(4) Different packages and not subclasses: (NO, NO…)

This is all the test code displayed. You can also test it yourself like me, which makes it easier to remember and understand.

Through this we can draw the conclusion: We understand why subclasses can only access the parent class using public and < strong>protectedModified properties and methodsThe properties and methods modified by the default modifiers default and private in the parent class cannot be accessed by the subclass. But be sure to note that the subclass inherits all members of the parent class, but some are inaccessible due to access modifiers.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 139,101 people are learning the system