[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:

public trong java
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:
khả năng truy nhập public

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
protected
Reviews:
khả năng truy nhập protected

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.
default

Reviews:
khả năng truy nhập default

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.

private

*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:
khả năng truy nhập private

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:

modifier

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.