In encapsulation , a classes variables will be hidden from other classes, and can only be accessed through the methods of their current class.
Hence, it is also known as data hiding. Encapsulation is one of the four fundamental OOP concepts.
The other three are inheritance , polymorphism and abstraction.
For ex:
class Learner{
private String name;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
}
public class EncapsulationEx{
public static void main(String[]args){
Learner l=new Learner();
l.setName("Ayush");
System.out.println(l.getName)
}
}