Java Encapsulation Example

Encapsulation is a process of wrapping of data and methods in a single unit is called encapsulation.

Java Encapsulation Example

Let's create a Person class to demonstrates the use of encapsulation in Java.

Step 1: Create a Person class.
public class Person {

    private double id;
    private String name;

    public Person() {
        // Only Person class can access and assign
        id = Math.random();
        sayHello();
    }

    // This method is protected for giving access within Person class only
    private void sayHello() {
        System.out.println("Hello - " + getId());
    }

    public double getId() {
        return id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
Step 2: Let's test the Encapsulation via a main() method.
public class EncapsulationDemostration {
    public static void main(String[] args) {

        Person p1 = new Person();
        p1.setName("Ramesh");
        /*
         * Note: As id and name are encapsulated in Person class, those cannot be accessed 
         * directly here. Also there is no way to assign id in this
         * class. Try to uncomment below code and you would find compile time error.
         */
        // p1.id = "123";
        // p1.name = "this will give compile time error";
        // p1.sayHello(); // You can't access this method, as it is private to Person

        System.out.println("Person 1 - " + p1.getId() + " : " + p1.getName());
    }
}

Reference



Related Java OOPS Examples


Comments