I know this is very basic but I want to know why we should use private variable in encapsulation.I am mentioning my code. so I can give you better explanation.
Class Employee
{
private String eName;
public String getEname()
{
return this.eName;
}
public void setEname(String name)
{
this.eName=name;
}
Here "eName" is private so we can restrict outside object to access directly. variable can be access using its getter setter only.
Now my question is why we should use getter setter method? can't we declare variable as a public.?
Using setter method any one can change the value so what is need of this private variable?
Another Question
we set create read/write only method if we don't create getter method then it become write only and if we don't create setter method then it become read only.
so what is the use of read only and write only ?
If we don't create setter method then how value will set to variable ?
If we don't create getter method then how value will retrieve?
Please give me the answer of above simple questions.
Thank you :)