The difference between 4 modifiers in Java and the whole process of explanation

Source: Internet
Author: User
Tags modifiers

The difference between 4 modifiers in Java and the whole process of explanation

Http://jingyan.baidu.com/article/fedf0737700b3335ac8977ca.html

The modifiers in Java 4 are public, protect, default, private, which explains the object-oriented encapsulation, so we want to apply them as much as possible to minimize the permissions, thereby improving security.

The following is a detailed discussion of their access rights. (All modifiers in the same class except the inner class are accessible, so this is excluded below.) )

The first thing to do is to show their access rights here:

Access Rights class Bun class other packages

Public∨∨∨∨

Protect∨∨∨x

Default∨∨xx

Private∨xxx

The following is a further explanation of the differences between the 4 modifiers in Java and the whole process of access:

Tools/Materials
    • Java Runtime Environment

    • notepad++

Method/Step
  1. First, explain the permissions problem of public, define 4 class person, people, Student, computer, compare the difference of this class, same package, subclass, other package respectively.

    It is important to note that when overriding the parent class member function, the member function of the subclass cannot be smaller than the member function permissions of the parent class, and the general override defines the permissions as the same.

    Code:

  2. The upper class is built, and then the test, if all can be compiled through, it means that the public decorated classes in this class, the same package, subclasses, other packages are allowed to access each other. The test results are as follows: (proven to be accessible)

  3. Again to explain the Protect permissions problem, the same definition of 4 classes of person, people, Student, computer, respectively, compare this class, the same package, sub-class, other packages of the difference.

    It is important to note that public can modify any class and that the class name is the same as the file name, protected not be able to decorate the class.

    The person code, the other 3 similarities:

  4. Also start testing protected permissions problem, if person, people, student can compile through, it means that the class with protected decorated in this class, the same package, sub-class access to each other is possible, The computer compilation does not pass the description protected cannot access each other in a class that does not have an inheritance relationship outside the package. The test results are as follows:

  5. Then we explain the default permission problem, and also define 4 classes of person, people, Student, computer, respectively, compare the difference of this class, the same package, subclass, other package.

    Note here: default, which does not write defaults, can modify any class as public and the class name is the same as the file name.

    The person code, the other 3 similarities:

  6. Continue to start testing the default permissions problem, if the person, people can be compiled through, it means that the default decorated classes in this class, the same package access to each other is possible, and student, Computer compilation does not pass the description that the default cannot be accessed outside the package, regardless of whether the class has an inheritance relationship. The test results are as follows:

  7. Finally, the private permission problem, the same definition of 4 class person, people, Student, computer, respectively, compare this class, the same package, sub-class, other packages of the difference.

    It is important to note that private and protected can not modify the class.

    The person code, the other 3 similarities:

  8. Finally, the private permission problem is tested privately, if the person can be compiled by the people, Student, computer compilation does not pass, it means that the class with private decoration can only be accessed in this class. The test results are as follows:

  9. In summary, Java 4 modifiers access to the difference and explain the whole process of this demonstration, once again the Java language Rigor and security, we later use the 4 modifier when creating a class, this class of permissions as small as possible, so as to reduce vulnerability and improve security, This embodies the Java object-oriented encapsulation.

    Finally, recall the access permissions for the 4 modifiers in Java:

The difference between 4 modifiers in Java and the whole process of explanation

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.