Java Encapsulation

In this tutorial, we will learn about encapsulation and how encapsulation of the OOPs can be achieved in the java with working examples and importance of it.

What is an encapsulation

Encapsulation is a concept, which refers to binding of the data members and methods which operate on data members in a single class. The data and methods will not be exposed to the outside of the class this in turns provides security to the data and helps to avoid manipulating the data.

One such real example can be an online banking transaction, data of the bank can be accessed or modified inside of the bank website not outside of it.

Encapsulation can be achieved in Java with two steps.

  1. Declaring all the data and data operating methods inside a single class
  2. Restricting the access to the operating methods and data members by limiting its access.

Encapsulation implementing in Java

Following are step by step explanation about writing Java code by obeying the principle of Encapsulation.

Step 1:

Note : Access specifier of a class can be anything, in case need to use private class then embedding as a nested class will be good example of encapsulation. For this article, we will be using a nested class as private access specifier.

In this step, we are declaring a nested class(Class inside a class).

public class MainClass {
	private class EncapsulationClass {
	}
}

In above nested class, outer class(MainClass) will be with public access and inner class(EncapsulationClass) with private access.

Step 2:

In this step, we are declaring a method inside of inner class to perform a login operation.

public class MainClass {
	private class EncapsulationClass {
		private boolean login(String userId,String password) {
			return true;
		}	
	}
}

In the above declared a method login and returning true by default.

Step 3:

In this step, we will initialize EncapsulationClass and call the login method with username and password.

public class MainClass {
	public boolean attemptLogin(String username,String password) {
		EncapsulationClass encap = new EncapsulationClass();
		return encap.login(username ,password);
	}
	private class EncapsulationClass {
		private boolean login(String userId,String password) {
			return true;
		}	
	}
}

Step 4:

In this step, we will see any data members or methods of EncapsulationClass can be accessed.

CopiedCopy Code
public class MainClass {
	public EncapsulationClass encap;
	MainClass() {
		this.encap = new EncapsulationClass();
	}
	public boolean attemptLogin(String username,String password) {
		return encap.login(username, password);
	}
	private class EncapsulationClass {
		public String apiURL;
		private String apiSessionCode;
		private boolean login(String userId,String password) {
			return true;
		}	
	}
	public static void main(String args[]) {
		Outside out = new Outside();
		out.accessEncapsulation();
	}
}
class Outside {
	public void accessEncapsulation() {
		MainClass main= new MainClass();
		//The below lines can use the access to the encapsulationclass object
		System.out.println(main.encap);
		//none of Encapsulationclass members are accessible 
		System.out.println(main.encap.apiURL);
                              // The above line cause compilation error
	}
}

If we try to compile the above code, we will get the compilation error as below.

The type MainClass.EncapsulationClass is not visible

Although apiURL declared with public access, still not visible outside of MainClass and this a classic example of Encapsulation.

Use cases of encapsulation:

  1. Can be used in frameworks, and there by helps to avoid interference or misuse on outside of framework.
  2. Where hiding of implementation logic is needed for security purpose.

Conclusion:

In this tutorial, we have learned about what is an encapsulation , a working example of how it can be achieved in Java and its use cases.