Encapsulation in Java


Encapsulation hides the internal state of an object and requires all interaction to happen through an object’s methods, enhancing security and flexibility.

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