Encapsulation means hiding the implementation details from the outside world. Below image shows how encapsulation is achieved in Java by declaring the private members of the class. Remember that We can not access the private members from outside of the class. Thus, any changes made inside the class with respect to private members do not impact outside classes.Below example illustrates how encapsulation works in Java.

package encapsulation;

 * Created by ssalunke on 19/04/2016.
public class Encapsulation {
    public static void main(String [] args){

        A a1 = new A();

        //We can not access private members from here
        //Below line will throw compilation error saying a has private access
        //Below line will throw compilation error saying seta has private access

        //We can access public members from outside of class

class A{
    private int a;
    private String s;

    private void seta(int a){
        this.a = a;

    public void display(){

Here is the output of above code.


Web development and Automation testing

solutions delivered!!