Encapsulation in java


☞The process of wrapping data and function together as a unit is known as Encapsulation.

☞It can be made possible by declaring the class members private.

Example

import java.util.*;

class EncapsulationExample{
    
    private String name;
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name=name;
    }
    
    public static void main(String[] args) {
        EncapsulationExample obj = new EncapsulationExample();
        obj.setName("inspirewebsoft");
        System.out.println("Name is : "+obj.getName());
    }
    
}

Output

Name is : inspirewebsoft