Wednesday, April 2, 2014

JAVA CONCEPTS, Modifier - Part 3

In the last post of modifier series, I have explained about the Public access modifier 
In this post i will explain about the private access modifier. 

Private
  1. Talking about the private access modifier we can relate it with something like, a higher delegate of a company(like CEO or VP) having meeting with few company's employee inside a room, that means at that moment delegate will be accessible to the employees inside that room only.
  2. private access modifier provides maximum restriction with less visibility and accessibility.
  3. private access modifier can be used with the class members only(variables and methods)
  4. we can't use private access modifier with top level class name , we can use private access modifier with class in nested class condition(inner class).
  5. when we use private access modifier with class members then that class members can be used in that class only where that is declared and defined. that class members can't be used any where else in java world.
  6. you can use private access modifier with constructor, singleton design principles(i will explain in upcoming post).
Example :
To understand the affect of private access modifier try to use this with the class members like variables and methods and then try to access that from different packages or different classes and note the problem you will be facing.
package packageParent;

public class ParentClass {
	private int parentVariableA; // variable with private accessifier
	public int parentVariableB; // variable with public accessifier

	public void parentMethodA() {  // public method
		System.out.println("do something");
	}

	private void parentMethodB() {  // private method
		System.out.println("i cant be accessible");
	}

}

Now let us write an another class in same package
1. class OtherClass {
2.	public static void main(String[] args) { // here we should not write main method 
3.		ParentClass pc = new ParentClass();
4.		System.out.println(pc.parentVariableB);
5.		System.out.println(pc.parentVariableA);// compiler will complain
6.		pc.parentMethodA();
7.		pc.parentMethodB(); // compiler will complain
	}
}

At line no 2: here we can't write main method because if we have a source file with public class as well as class without public access modifier(called as default modifier or package level modifier), then main method should always be present inside the class with public access modifier. here compiler will not complain but java runtime environment will complain.

At line no 3: we create an object of ParentClass to access the members of ParentClass.

At line no 4: we are trying to access public variable and compiler is ok with this
At line no 5: we are trying to access private variable of ParentClass then compiler can't access that variable as private means visible in same class only irrespective of package or any inheritance tree.
At line no 7: same reason as line 5 as compiler will complain here also.

summary:
As private access modifier is highly restrictive with less visibility, so either we write any class in same package or in different package , or we try to use inheritance concept to access private variables, it will never be possible to access private members outside that class where that is declared.

but you must be thinking what is use of private then, if we can't access it from other part of application ? 
so here it relates an important oops concept called as proper encapsulation. make the variables private and try to access those variables using methods and modify those (this is called JavaBean style). I will explain this concept in OOPs concept.

No comments:

Post a Comment