Access permission control in java Encapsulation

Source: Internet
Author: User

Access permission control in java Encapsulation

EncapsulationLiterally, packaging means information hiding. It means encapsulating data and data-based operations using abstract data types, make it an inseparable and independent entity. The data is protected inside the abstract data type, and the internal details are hidden as much as possible, so that only some external interfaces are kept in touch with the external. Other objects in the system can communicate and interact with the encapsulated object only through authorized operations wrapped outside the data. That is to say, the user does not need to know the internal details of the object (of course, they do not know), but can access the object through the interface provided by the object.

For encapsulation, an object encapsulates its own attributes and methods, so it can complete its operations without relying on other objects.

Encapsulation has four advantages:

1. Good encapsulation can reduce coupling.

2. The internal structure of the class can be freely modified.

3. More precise member control.

4. Hide information and implement details.

Java encapsulation information has four Access PermissionsPackage access permissions: public, protected, and private. Note: The class has only two Access PermissionsThe default access permission and public. The four access permissions are usually used for its internal member variables and member methods. One Members inside the class, whether private or protected, can access each other directly.. The four types of access permissions usually refer to the access permissions of the class members through the class objects. Because the interface provided by the Program is an object. Perform specific operations through the object class. For example: This member variable or method has the package access permission, which means that the object of this class can access it freely in the package (member variable/member method) Public> protected> package access permission> private 1. public It means that the object of this class can access the member variables or methods with public in any place. To ensure that methods and variables can be accessed by the producer, you must first ensure that the class can create objects across packages, that is, the class is public first. of course, this class should also be public first, otherwise it will still not be accessible in different packages, because this class cannot create objects first, even if it is imported. If this class is the default permission, even if the class member variable or method is public, it cannot be accessed outside the package, but only in the package.
Package jin. feng1; public class Cookie2 // this class must be public; otherwise, the class cannot be accessed {public int I; public Cookie2 (int I) {this. I = I;} public void print () {System. out. println ("I:" + this. i) ;}} package jin. feng2; import jin. feng1.Cookie2; public class Access {public static void main (String [] args) {Cookie2 cie2 = new Cookie2 (3); cie2.print ();}}

2. protectedIf a new package is created and the class is inherited from the other package, the only accessible member is the public Member of the source package. Sometimes the class Creator wants to have a specific member, grant its access permission to the derived class instead of all classes. This requires protected. protected to also provide package access permissions. Protected provides two access methods: Object Access and direct access to Members in the base class within a derived class.
Why is the previous access only through objects because the two classes do not establish a relationship.
Package jin. feng1; public class Cookie2 {protected int I; public Cookie2 (int I) {this. I = I;} protected void print () {// package access permission System. out. println ("I:" + this. i) ;}} package jin. feng2; import jin. feng1.Cookie2; public class Access extends Cookie2 {public Access () {// if the base class constructor has parameters, the derived class must display the declared constructor. Super (3); print (); // class internal direct access, derived class direct access} public static void main (String [] args) {// Cook1 cook1 = new Cook1 (1, 2); Access asAccess = new Access (); asAccess. I = 9; // access through an object. AsAccess. print ();}}

3. Package access permission (default access permission, not written ): This member variable or method has the package access permission, which means that the object of this class can access it freely in the package (member variable/member method)Package access permission
Class Cook1 {int I; // packet access permission int j; // packet access permission public Cook1 (int I, int j) {this. I = I; this. j = j;} void print () {// package access permission System. out. println ("I:" + this. I + "j:" + this. j) ;}} public class Access {public static void main (String [] args) {Cook1 cook1 = new Cook1 (1, 2); cook1.print (); // The object of this class can access member variables with package access permissions in the package/method cook1. I = 9; // The object of this class can access member variables with package access permissions in the package/method cook1.print ();}}
4. privateThis class object cannot access this class with private members.
Class Cook1 {private int I; int j; public Cook1 (int I, int j) {this. I = I; this. j = j;} private void print () {System. out. println ("I:" + this. I + "j:" + this. j) ;}} public class Access {public static void main (String [] args) {Cook1 cook1 = new Cook1 (1, 2); cook1.print (); // error, the private member method cook1. I = 9 is not accessible; // An error occurred while this type of object cannot access the private member variable cook1.j = 10;

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.