Java – Encapsulation in Java

Encapsulation in Java

Encapsulation is a protective mechanism by which members of a class (method and variables) are prevented from being accessed by members of other class.  

The best example of encapsulation is the class because that binds its variables and methods and hides their complexity from other class.  

In a class, the general variables and methods are defined or marked as private, protected and the public to bind the data from unauthorized access.

Java Source

public class Inclass {
private String name;
private String subject;

public void hola(String n, String sn){
name=n;
subject=sn;
System.out.println(name+"'MyName:'"+subject+"'I Love'");
}

public static void main(String[] args) {
Inclass s = new Inclass();
s.hola("Satya", "Math");
}

}