I'm studying about encapsulation. We can create a fully encapsulated class in java by making all the data members of the class private.
We do that so that nobody else could access my class variables directly and change them.
For Example:
Class MyAccount{
int AccountTotalAmt = 1000;
}
Class Mainclass{
public static void main(String[] args){
MyAccount Obj = new MyAccount();
obj.AccountTotalAmt = -200; (Just to avoid this kind of situations. Its just an example could be many more reasons)
}
}
Like I said , We do that because "nobody else could access my class variables directly and change them." To access them we make the variable private and set them using getter and setters. So what makes a difference here? What if someone knows these setters and getters as well? Can change them easily that way.
Is it a foolproof plan? Can anyone explain in a better way?