Encapsulation literally is the meaning of packaging, the professional point is information hiding, refers to the use of abstract data types to encapsulate data and data-based operations together to form an indivisible independent entity, the data is protected in the abstract data type inside, as far as possible to hide the internal details, Only some external interfaces are retained to make them contact external. Other objects of the system can communicate and interact with this encapsulated object only through authorized operations that are wrapped outside the data. This means that the user does not need to know the details inside the object (and of course it is unknown), but can access the object through the interface provided by the object externally.
For encapsulation, an object encapsulates its own properties and methods, so it does not need to rely on other objects to do its own work.
There are four advantages to using encapsulation:
1, good package can reduce the coupling.
2, the structure within the class can be freely modified.
3, the member can be more precise control.
4, hide information, realize the details.
Java encapsulates information with four access rightspackage access, public,protected, and private. Note: The
class has only two access rights, default access rights and public. The four types of access that you usually say are primarily for member variables and member methods within it.
members within a class can be accessed directly from each other, whether they are private,protected or not. The four types of access typically refer to the permission to access a member of that class through that class of object. Because the interface that the program provides externally is the object. Implement specific actions through object classes. such as:
the member variable or method has the package access permission refers to the object of the class can freely access it within the package (member variable/member method)
public>protected> Package access rights >private
1. Public
is that the object of the class can access any member variable or method of the class with public anywhere. To ensure that the methods and variables can be accessed by the Satchel, first ensure that the class is able to create objects across the package, that is, the class is first public. Of course, this class should also be public, otherwise it is not accessible in different packages, because first this class cannot create objects, even if import does not work. If this class is the default permission, even if the class's member variable or method is public, it cannot be accessed outside the package, only within the package.
Package jin.feng1; public class Cookie2//This class must be public otherwise the class cannot access the {public int i, public Cookie2 (int i) {this.i=i,} public void print () {System.ou T.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 you create a new package and inherit a class from another package, the only member that can be accessed is the public member of the source package, and sometimes the creator of the class wants to have a particular member, giving it access to the derived class instead of all classes. This requires that protected.protected also provide package access. There are two ways to access protected, one is through object access, and the other is by directly accessing members inside a base class within a derived class.
Why was the previous access only through objects, because two classes did 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 SYSTEM.OUT.PRI Ntln ("I:" +this.i);}} Package Jin.feng2;import Jin.feng1.cookie2;public class Access extends Cookie2 {public access () {///if the constructor of the base class has parameters, The derived class must display the declaration constructor. Super (3); Print (); Direct access to the class, derived classes directly access} public static void Main (string[] args) { //cook1 cook1=new Cook1 (1, 2); Access asaccess=new access (); asaccess.i=9; Access through objects. asaccess.print ();}}
3, in-Package access rights ( Default access rights, not written):
the member variable or method has a package access permission refers to an object of that class that can freely access it within the package (member variable/member method)In-Package access rights
Class cook1{int i;//package Access int j;//package access rights public Cook1 (int i,int j) { this.i=i; This.j=j;} void print () {//package access 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 ();//objects of this class can access the package within the package Member variable/method of Access cook1.i=9;//the object of the class can access a member variable/method Cook1.print () with package access within the package; }}
4. PrivateThe class has private members that cannot be accessed through the class object.
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, inaccessible Private member Method cook1.i=9;//error, the class object cannot access the private member variable cook1.j=10;
Access control in the Java package