In the previous tutorial, we have learned about encapsulation, data hiding, and tightly encapsulated class in java with a lot of real-time examples and programs.

If you have any doubts related to the concepts of encapsulation, I will recommend you first visit the encapsulation tutorial. The link is given below:
Encapsulation in Java with Real-time Examples Let's discuss more encapsulation example programs in java with step by step explanations.

Program source code 1: 
In this example program, we will set one field as private in the class Number. So, the field can be accessed only within the class and no anyone can access from outside the class. 

After that, we will use public setter and getter methods to set and read the value of the variable or field. By doing this, we are hiding the field and providing its implementation from outside the class. This programming mechanism is called Encapsulation in Java.

It helps to bind the data (variables) and code (methods) together with them. For example, a class is a good example of encapsulation because if we write variables and methods inside the class, the class binds them together.

    Step1: Create a package as 'numberTest' and make a class 'Number'. package encapsulationTest; public class Number{ Step2: Declare field/variable as private. private int y; Step3: Create a public getter method for each private variables. public int get(){ return y; } Step3: Create a public setter method for each private variables and declare the parameter. public void set(int y){ this.y=y; } } Step4: Create another class EncapTest. public class Encaptest{ public static void main(String[] args){ Step5: Create an object of class Number using the new keyword. Number n=new number(); // Here, n is a reference variable of Number and pointing to the object of class Number. Step6: Now call the setter method and set the value of the variable. n.set(6); Step7: Call getter method to read the value of variable. int num=n.get(); Step8: Print the output. System.out.println(num); } }
    Output: 6
In the preceding example program, the variable y is made private. This variable can be accessed and manipulated only using set() and get() methods.

Thus, we can say that here the variable y and the functions get() and set() are bonded together in a class which represents Encapsulation.

Common mistakes while implementing getter and setter method:

Consider the above code snippet, if the variable y is declared as public then methods can be called and used directly using dot (.) operator from anywhere outside the class and making the setter and getter useless.

So, you always declare more restrictive access modifiers such as protected and private.
For example:
    protected String name; private int num;
Getter and Setter in Java | Example Programs Follow all the above steps to make the coding snippet of Encapsulation easy.
Program source code 2:
Let us understand one more example program. In this example program, we will make a class 'Rectangle' and declare the variable length and breadth as private. One Constructor will be used for initializing the value of variables. 

When the object is created, the constructor will be called and the default value of the instance variables will be assigned.
    Package encapsulationTest; public class Rectangle { // Declare instance variables as private in the class. private int length; private int breadth; // Declare a constructor Rectangle and define parameters of constructor. Rectangle(int length, int breadth){ this.length=length; this.breadth=breadth; } // Create a getter method for each private variables. public int getLength(){ return length; } public int getBreadth(){ return breadth; } // Create a setter method for each private variable and define parameter. public void setLength(int length){ this.length=length; } public void setBreadth(int breadth){ this.breadth=breadth; } }
Now create another new class 'RectangleTest' and follow all the steps as shown in the program.
    package encapsulationTest; public class RectangleTest { public static void main(String[] args){ // Create an object of class Rectangle and assign the values of the parameter used in the constructor. Rectangle rt=new Rectangle(20,30); // Call the getter method to read the value of variable because we cannot read the value directly due to privacy. int lth=rt.getLength(); int bth=rt.getBreadth(); // Calculate area of the rectangle and print it on the console. int Area=lth*bth; System.out.println("Area: " +Area); //, let's update the new value of the variable using the setter method. rt.setLength(50); rt.setBreadth(60); // Call the getter method to read the updated value. int ln=rt.getLength(); int br=rt.getBreadth(); int newArea=ln*br; System.out.println("New area: " +newArea); } }
    Output: Area: 600 New area: 3000
Program source code 3: 

In this example program, we are creating a class Student and declare variables stdName, stdRollNo, and stdId as private. Let's see the following source code.
    package encapsulationTest; public class Student { // Step 1: Declare variables private in the class. private String stdName; // private field. private int stdRollNo; private int stdId; // Step 2: Apply public getter method for each private vairables in the class. public String getStdName(){ // private field accessed inside public method. return stdName; } public int getStdRollNo(){ return stdRollNo; } public int getStdId(){ return stdId: } // Step 3: Apply public setter method for each private variables in the class. public void setStdName(String name){ stdName=name; } public void setStdRollNo(int rollNo){ stdRollNo=rollNo; } public void setId(int id){ stdId=id; } }
    public class EncapTest { public static void main(String[][] args){ // Step 4: Create the object of class Student by using the new keyword. Student obj=new Student(); // Here, obj is reference variable of class Student and pointing to the objects of the class Student. // Step 5: Call setter method and set the value of variables. obj.setStdName("Kiran"); obj.setStdRollNo(4); obj.setStdId(12345); // Step 6: Call getter method to read the value of variables and print it on console. System.out.println("Student's Name: " +obj.getStdName()); System.out.println("Student's Roll no.: " +obj.getStdRollNo()); System.out.println("Student's Id: " +obj.getStdId()); } }
    Output: Student's Name: Kiran Student's Roll no.: 4 Student's Id: 12345
Program source code 4:
If we do not define the setter method in the class, the fields can be made read-only. Let's take a program where we will create a class 'Player' in which all the fields are declared as private. 

A constructor will be declared for initializing the parameters because we will not declare setter method in the class. So, we will use constructor to initialize value of variables.
    package encapsulationTest; public class Player; // Declare the variable as private. private String pName; private int pAge; private String gameType; // Create a constructor and initialize the three parameters pName, pAge, and gameType. public Player(String pName, int pAge, String gameType){ this.pName=pName; this.pAge=pAge; this.gameTypte=gameType: } // Apply the getter method for the private variables in the class. public String getPName(){ return pName; } public int getPAge(){ return pAge; } public String getGameType(){ return gameType; } }
    public class PlayerTest { public static void main(String[] args) { // Create an object of class Player and initialize the value of the parameters. Player ply=new Player("Sachin", 40, "Cricket"); // Call getter method to read the value of the variables. String plyName=ply.getPName(); int plyAge=ply.getPAge(); String plyGameType=ply.getGameType; System.out.println("Player's Name: " +plyName); System.out.println("Player's Age: " +plyAge); System.out.println("Player's Gametype: " +plyGameType); } }
    Output: Player's Name: Sachin Player's Age: 40 Player's Gametype: Cricket

Program source code 5:
If we do not define the getter method in the class then the fields can be made write-only. Let's make a program to make write-only. So, we will not define getter method and constructor because we can send directly the values of the parameter using the constructor.

Therefore, we will only define setter method in the class and set the value of parameters.
    package encapsulationTest; public class Student { // Declare variables as private. private String name; private int phyMarks; private int chemMarks; private int bioMarks; // Create setter method for all private variables. public void setName(String name){ this.name=name; } public void setPhyMarks(int phyMarks){ this.phyMarks=phyMarks; } public void setChemMarks(int chemMarks){ this.chemMarks=chemMarks; } public void setBioMarks(int bioMarks){ this.bioMarks=bioMarks; } // Create a method to print the output such as Student's Name, Marks in three subjects and percentage. void marksRead() { System.out.println("Student's Name: " +name); System.out.println("Marks in Physics: " +phyMarks); System.out.println("Marks in Chemistry: " +chemMarks); System.out.println("Marks in Biology: " +bioMarks); int Totalmarks=phyMarks+chemMarks+bioMarks; float percentage=(Totalmarks*100)/300; System.out.println("Percentage of PCB: " +percentage); } }
    public class StudentMarks { public static void main(String[] args) { Student st=new Student(); // Call the setter method and declare the value of the variables. st.setName("Vivek"); st.setPhyMarks(85); st.setChemMarks(75); st.setBioMarks(90); // Call the method to get the output. st.marksRead(); } }
    Output: Student's Name: Vivek Marks in Physics: 85 Marks in Chemistry: 75 Marks in Biology: 90 Percentage of PCB: 83.33
Final words
Hope that this tutorial has covered all the important concepts related to encapsulation in the example programs. I hope that you will have understood encapsulation concepts and have enjoyed programming. All programs are important to clear concepts.
Thanks for reading!