Write the Java Example for Encapsulation

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 the 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 Example for Encapsulation

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

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

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