What is Encapsulation?Encapsulation is the wrapping of data and code acting on data together in a single unit (class). The data is hidden from other class and can be accessed only through the methods of their current class. This way a class has total control over what is stored in its fields.
Example
public class Student { private String name; // any class that wants to access the variables should access them through getName and setName methods public String getName() { return name; } public void setName(String newName) { name = newName; } }
|