Encapsulation in Java with Example

In this chapter, we are going to look at examples and explanations of Encapsulation in Java.

Before starting encapsulation in Java, you need to understand access modifiers in Java.

Access Modifiers in Java:

As the name suggested, access modifiers are the keywords which block usage of variable, class, constructor, method or data member.

You can easily control the access of any variables as per your requirement. Let’s discuss different types of access modifiers.

  1. Default
  2. Public
  3. Private
  4. Protected

Public Access Modifier:

We can make any variable, class or data member public by using the public keyword.

  • If you’re making any data member public then it can be accessed from anywhere within a project.
  • public members have the widest scope and there is no restriction on the accessibility of a public member.
Public_Keyword_Java
  • In ClassC, print() method is a public method and any class from any other package can access it.

Default Access Modifier:

Remember, when you don’t put any of the above access modifiers with any variable, class or data member then it is said to be having default access modifier.

  • default variable, class or data member can be accessed through the same package only.
Default_Keyword_Java
  1. No access modifier is defined, therefore it is a default method.
  2. In the different package, we can’t access the default method or data member. Therefore we can only access it within the same package.
  3. Eclipse IDE is also suggesting that method print() is not visible and you should make it public because the public method can be accessed from anywhere.

Private Access Modifier:

We can make any variable, class or data member private by using the private keyword.

  • If you’re making any data member private then it can be accessed within class only.
  • Also, you can’t make top-level classes or interface as private because.
    1. If you do so then it is impossible to extend or implement any interface.
  • Even any other class of the same package can’t access private member.
Private_Keyword_Java

Protected Access Modifier:

We can make any variable, class or data member protected by using the protected keyword.

  • If you’re making any data member protected then it can be accessed within the same packages or subclasses.
  • During inheritance, the protected members from the parent class can be easily accessible from the subclass.
Protected_Keyword_Java
  • We can access a protected method from the subclass.

Encapsulation in Java:

Hope you have learned about access modifiers now we are going to look at how encapsulation in Java works:

Encapsulation means binding of data into a single unit that means you can create a class and set all of your code there and hide it from the user.

In other words, encapsulation is hiding of data and implementation from the user.

I know definition is quite similar to Abstraction i.e hiding the implementation. We’ll discuss further the difference between encapsulation and abstraction.

So how you’ll hide the data? Yes, by using access modifiers. If you declare any member private then you’re hiding it from the outer world.

However, you can make your data members private or protected and can provide public getters and setter methods to get and set value from other classes.

In this way, data can be accessed through public methods.

package package1;

public class EmpData {

//encapsulated data members
	private int empCode;
	private String empName;

//Getter and Setter methods
	public int getEmpCode() {
		return empCode;
	}

	public void setEmpCode(int code) {
		empCode = code;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String name) {
		empName = name;
	}

}
package package1;

class CompanyData extends EmpData {

	public static void main(String args[]) {
		CompanyData obj = new CompanyData();
//setting empdata with setters
		obj.setEmpCode(123);
		obj.setEmpName("Shubham Chaudhary");

//getting empdata with getters
		System.out.println("Employee Code: " + obj.getEmpCode());
		System.out.println("Employee Name: " + obj.getEmpName());

	}
}

Advantages of Encapsulation in Java:

  • Encapsulation provides control over the data.
  • It maintains reusability and flexibility to the code.
  • Data Hiding can be achieved through encapsulation.
  • We can make a class read-only by using getters and write-only by using setter.

Difference between Encapsulation and Abstraction?

Encapsulation and Abstraction are the two pillars of Object-Oriented Programming and many beginners get confused while learning these OOPs concepts.

Because they look very similar in terms of definition and trust me they are far different in terms of usage.

As we all know encapsulation and abstraction both focus on hiding the details but let’s try to understand the difference.

  • Abstraction is the OOP concept that hides the unnecessary details and complexity from the user like an ATM machine.
  • Encapsulation is also OOP concepts that hide the data from the outer world i.e from a different class or different package.

Now it comes,

Abstraction hides the details or complexity and Encapsulation hides the internal working which can be easily changed afterward.

Abstraction hides details at the design level
  • While declaring the blueprint of a class i.e Interface. You’re hiding the details and that is design level.
Encapsulation hides details at the implementation level
  • While using any method in some other class you can hide its availability with the help of access modifiers. Therefore, it is the implementation level.

I hope you’ve understood Encapsulation in Java and basic difference between encapsulation and abstraction.