[Java] The level of access in java – Edit in Java
To protect data prevent free access from outside, object-oriented programming using the keywords specified range access the properties and methods of the class. Java uses some later complements to control access to the components of the object :
- public: Ingredients public, free access from outside.
- protected: Ingredients are protected, restricted access.
- default (do not write anything): Access within the package.
- private: Access within the class.
1. The public member
Ingredients public sector is that class can share with all the programs and external audiences. Public member can be accessed from inside and outside the classroom:
Example: It is structured 2 packages as follows:
In which the content of the file we are as follows:
* ClassA1.java file packaged with the components GoiA (varA1 variables and methods methodA1) the public (public):
package GoiA; // goi ten la GoiA public class ClassA1 { public int varA1 = 5; // bien cong khai ten la varA public void methodA1(){ // phuong thuc cong khai methodA1 System.out.println("methodA1 trong ClassA1 la public"); } }
* ClassA2.java file included in the package GoiA 1 methodA2 methods used by the components of ClassA1.
package GoiA; // goi GoiA public class ClassA2 { // ClassA2 su dung bien va phuong thuc cua ClassA1 void methodA2(){ // phuong thuc methodA2 goi cac thanh phan cua ClassA1 ClassA1 a1 = new ClassA1(); // tao doi tuong a1 thuoc lop ClassA1 a1.methodA1(); // goi phuong thuc methodA1 System.out.println("Bien varA1 = " + a1.varA1); // in bien varA1 } }
* ClassA3.java file is located in GoiA inherit and use the components of ClassA1
package GoiA; public class ClassA3 extends ClassA1{ // ClassA3 ke thua ClassA1 void methodA3(){ // phuong thuc methodA3 goi methodA1() methodA1(); } }
Switch GoiB packages have 2 file:
* ClassB1.java file also called components of ClassA1 like ClassA2 file is different however because ClassB1 package with ClassA1 so we need to import it.
package GoiB; import GoiA.ClassA1; // import ClassA1 de su dung public class ClassB1 { void methodB1(){ // phuong thuc methodB1 goi cac thanh phan cua ClassA1 ClassA1 a1 = new ClassA1(); // tao doi tuong a1 thuoc lop ClassA1 a1.methodA1(); // goi phuong thuc methodA1 System.out.println("Bien varA1 = " + a1.varA1); // in bien varA1 } }
* ClassB2.java file also inherited legacy ClassA1 like ClassA3 ClassA1 but need to import ClassA1.
package GoiB; import GoiA.ClassA1; public class ClassB2 extends ClassA1{ // ClassB2 ke thua ClassA1 void methodB2(){ // phuong thuc methodB2 goi methodA1() methodA1(); } }
Thus we can see the openness of ClassA1 everywhere when we use public complements.
We can draw quick remarks following illustrations:
2. The components protected
All classes in the package that contains it and all classes inherit it (including classes inherit from other packages) will have access to it.
Also for example as above, we have 2 package is GoiA and GoiB with the class that, just change ClassA1 protected with the following components (Các ClassA2, ClassA3, ClassB1, ClassB2 remain):
*flie ClassA1.java
package GoiA; // goi ten la GoiA public class ClassA1 { protected int varA1 = 5; // bien protected ten la varA protected void methodA1(){ // phuong thuc protected methodA1 System.out.println("methodA1 trong ClassA1 la public"); } }
Then the class ClassA2, ClassA3 still have access to the components of ClassA1 as it is also known GoiA with ClassA1, ClassB2 but also accessible for other packages, but it inherits ClassA1. However ClassB1 not accessible.
*flie ClassB1.java
package GoiB; import GoiA.ClassA1; // import ClassA1 de su dung public class ClassB1 { void methodB1(){ // phuong thuc methodB1 goi cac thanh phan cua ClassA1 ClassA1 a1 = new ClassA1(); a1.methodA1(); // loi truy nhap System.out.println("Bien varA1 = " + a1.varA1); // loi truy nhap } }
ClassB1 faulty
Reviews:
3. The default component
The default components (What undeclared) it only allows the innermost layer 1 Access to package (even legacy). Any access or inherited from other packages are not.
Similarly, in our example to the composition of the default ClassA1, Meanwhile ClassB1 and ClassB2 are faulty because it is not in the same package with ClassA1.
* ClassA1.java file
package GoiA; // goi ten la GoiA public class ClassA1 { int varA1 = 5; // bien mac dinh ten la varA void methodA1(){ // phuong thuc mac dinh methodA1 System.out.println("methodA1 trong ClassA1 la public"); } }
*flie ClassB1.java
package GoiB; import GoiA.ClassA1; // import ClassA1 de su dung public class ClassB1 { void methodB1(){ // phuong thuc methodB1 goi cac thanh phan cua ClassA1 ClassA1 a1 = new ClassA1(); a1.methodA1(); // loi truy nhap System.out.println("Bien varA1 = " + a1.varA1); // loi truy nhap } }
*flie ClassB2.java
package GoiB; import GoiA.ClassA1; public class ClassB2 extends ClassA1{ // ClassB2 ke thua ClassA1 void methodB2(){ // phuong thuc methodB2 goi methodA1() methodA1(); // loi truy nhap } }
ClassB1, ClassB2 are faulty ClassB2 although we can declare that extends ClassA1 but also unusable components ClassA1.
Reviews:
4. The private sectors
The private sectors are most closely guarded, only objects of the same 1 New classes accessible, all access or inherit from other classes even in the same class 1 package is not accessible.
*flie ClassA1.java
package GoiA; // goi ten la GoiA public class ClassA1 { private int varA1 = 5; // bien private ten la varA private void methodA1(){ // phuong thuc private methodA1 System.out.println("methodA1 trong ClassA1 la public"); } void methodA2(){ methodA1(); // goi phuong thuc methodA1 System.out.println("Bien varA1 = " + varA1); // in bien varA1 } }
We see only method methodA2() Class is the same with the private component of ClassA1 can access, even ClassA2, ClassA3 is the same package can not access GoiA.
Reviews:
Thus we can see that the accessibility of the public sectors, protected, default, private means decreasing the security of information increases. We can have a small sum after:
Posts with reference to the:
– Curriculum Java object-oriented programming – Text
– Curriculum Java object-oriented programming – Tran Dinh Que
– naynhoanhthichem.blogspot.com
And some textbooks, other sites.
Cám ơn bro đã có giải thích rất trực quan 🙂
Visit the blog regularly ^^
Cam on Nguyenvanquan7826 rat hay, valve replacement ever did these attributes mung lung. Read this article now tastes absolutely 100%. There…Obvious.
His explanation is straightforward
Thank you.
Thank you very much. Extremely easy hieu.Tham article even just see illustrations below line “Reviews:” also understand and retentive then.
Thank you. Refer to a friend offline.
You ask yourself not directly call functions from your class inherits à DC. Example:package GoiA;
public class extends ClassA3 ClassA1{
void methodA3(){
methodA1();
}
}
In class on why I do not even call methodA1(); but need it in a method (here is mehodA3 ) So your other?
Call you… I mean we can call the methods of the superclass, however want to call, you need to give it to 1 or to use a function object to call.
let me ask method A in package 1 Kids Mode to public . So in package 2 you can call the method A is not it
Yes you.
Thank you very much. Very clear article, easy to understand. Chúc anh đạt được nhiều thành công và có nhiều bài viết hay như thế này nữa 🙂
Thank you.
Bài viết của anh rất hay!
Thank you
Bài viết rất hay và chi tiết. Thank you!
Thank you, hãy chia sẻ cho bạn bè nhé
thank you very much , page bạn giải thích còn dễ hiểu hơn là trên lớp dạy đấy
Cảm ơn bạn 🙂 Chia sẻ cho bạn bè nhé.
bài này hay quá anh ơi :D,anh cho em xin link bài giảng try catch luôn nha,em hơi thắc mắc khi nghe giáo viên giảng.Thầy cô nói try catch khi mình ko chắc code đúng nên dùng,mà em nghĩ nếu biết nó ko đúng thì sửa lại luôn chứ :))
hay hiểu 100% thank you
Chia sẻ cho bạn bè và ghé blog thường xuyên nhé bạn.