Encapsulation makes sure that sensitive stuff is hidden from users.
Idea: declare class variables/attributes as private, provide public getters and setters to use the variable.
public class Safe {
private String superPrivateInfo;
public String getSuperPrivateInfo() {
return superPrivateInfo;
}
public void setSuperPrivateInfo(String newInfo) {
this.superPrivateInfo = newInfo;
}
}
Classes can be:
public
: accessible by any other classAttributes/methods/constructors can be:
public
: accessible by any other classprivate
: only accessible within declared classprotected
: only accessible in same package and subclasses