Encapsulation in Java


Encapsulation allows you to protect the data of a class from external access.

Source Code

public class Person {
    private String name; // private = restricted access
    // Getter
    public String getName() {
        return name;
    }
    // Setter
    public void setName(String newName) {
        this.name = newName;
    }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments