What is Encapsulation in Java and How to Implement It?

Encapsulation in Java is a powerful mechanism for storing the data members and data methods of a class together. It is done in the form of a secure field accessible by only the members of the same class. This article will explore topics such as the encapsulation in Java, and how to implement it through the following docket. The topics that will be covered are:

  • What is Encapsulation in Java?
  • Need for Encapsulation in Java
  • Data Hiding in Java
  • Data Hiding vs. Encapsulation in Java
  • Getter and Setter Methods
  • Benefits of Encapsulation in Java

What is Encapsulation in Java?

java

Encapsulation in Java is an object-oriented procedure of combining the data members and data methods of the class inside the user-defined class. It is important to declare this class as private.

Next, we will understand the Syntax to be followed while implementing encapsulation in Java.

Syntax:

<Access_Modifier> class <Class_Name> {

 private <Data_Members>;

 private <Data_Methods>;

}

For enhancing the understanding of the encapsulation process, let us go through the following sample program.

Example:

package dc;

public class c 

{  

public static void main (String[] args) 

Employee e = new Employee(); 

e.setName("Robert"); 

e.setAge(33); 

e.setEmpID(1253); 

System.out.println("Employee's name: " + e.getName()); 

System.out.println("Employee's age: " + e.getAge()); 

System.out.println("Employee's ID: " + e.getEmpID()); 

 

package dc;

public class Employee {

private String Name;

private int EmpID;

private int Age;

public int getAge() {

return Age;

}

public String getName() {

return Name;

}

public int getEmpID() {

return EmpID;

}

public void setAge(int newAge) {

Age = newAge;

}

public void setName(String newName) {

Name = newName;

}

public void setRoll(int newEmpID) {

EmpID = newEmpID;

}

public void setEmpID(int EmpID) {

}

}

//Output:

Employee's name: Robert

Employee's age: 33

Employee's ID: 1253

Full Stack Java Developer Course

The Gateway to Master Web DevelopmentExplore Course
Full Stack Java Developer Course

Need for Encapsulation in Java

Encapsulation improvises the procedure of coding to a whole new level. We need encapsulation in Java for various reasons. They are stated below.

Encapsulation

Better Control

Encapsulation provides ultimate control over the data members and data methods inside the class.

Getter and Setter 

The standard IDEs provide in-built support for ‘Getter and Setter’ methods, which increases the programming pace.

Security

Encapsulation prevents access to data members and data methods by any external classes. The encapsulation process improves the security of the encapsulated data.

Flexibility

Changes made to one part of the code can be successfully implemented without affecting any other part of the code.

Data Hiding in Java

Data hiding is a procedure done to avoid access to the data members and data methods and their logical implementation. Data hiding can be done by using the access specifiers. We have four access specifiers, which are as follows.

Default

Default is the first line of data hiding. If any class in Java is not mentioned with an access specifier, then the compiler will set ‘default’ as the access specifier. The access specifications of default are extremely similar to that of the public access specifier.

Public

The public access specifier provides the access specifications to a class so that it can be accessed from anywhere within the program.

Example:

package Simplilearn;

class vehicle {

public int tires;

public void display() {

System.out.println("I have a vehicle.");

System.out.println("It has " + tires + " tires.");

}

}

public class Display {

public static void main(String[] args) {

vehicle veh = new vehicle();

veh.tires = 4;

veh.display();

}

}

//Output:

I have a vehicle.

It has four tires.

Private

The private access specifier provides access to the data members, and the data methods limit to the class itself.

Example:

package Simplilearn;

class Student {

private int rank;

public int getRank() {

return rank;

}

public void setRank(int rank) {

this.rank = rank;

}

}

public class school {

public static void main(String[] args) {

Student s = new Student();

s.setRank(1022);

System.out.println("Student rank is " + s.getRank());

}

}

//Output:

Student rank is 1022

Protected

The protected access specifier protects the class methods and members similar to the private access specifier. The main difference is that the access is limited to the entire package, unlike only a class with the private access specifier.

Example:

package Simplilearn;

class human {

protected String stream;

protected void display() {

System.out.println("Hello, I am a " + stream + " Student");

}

}

public class Student extends human {

public static void main(String[] args) {

Student s = new Student();

s.stream = "Computer Science and Engineering Technology";

s.display();

}

}

//Output:

Hello, I am a Computer Science and Engineering Technology Student

Data Hiding vs. Encapsulation in Java

Data Hiding

Data Encapsulation

Data hiding can be considered as the parent process

Encapsulation is a sub-process of data hiding

Access specifier is always private

Access specifier can be private and public

Data hiding is about hiding method implementation

Encapsulation is about combining methods with data members

The main motto is to hide data and its implementation

The main motto is to combine data and their methods 

Getter and Setter Methods

Getter

The method capable of accessing and retrieving an instance of a private variable is known as a getter method.

Setter

The method that is capable of modifying, or setting to an instance of a private variable is the setter method.

Example:

package Simplilearn;

public class Employee {

private String name;

public String getName() {

return name;

}

public void setName(String name){  

this.name=name ;

}

}

package Simplilearn;

public class GetSet {

public static void main(String[] args) {

Employee e = new Employee();

e.setName("Charles");

System.out.println(e.getName());

}

}

//Output:

Charles

Do you wish to become a Java Developer? Check out the Java Certification Training Course and get certified today.

Benefits of Encapsulation in Java

Implementing the process of encapsulation in Java has proven to be highly effective and beneficial while programming in real-time. The following are the significant benefits of encapsulation.

  • A class can have complete control over its data members and data methods.
  • The class will maintain its data members and methods as read-only.
  • Data hiding prevents the user from the complex implementations in the code.
  • The variables of the class can be read-only or write-only as per the programmer's requirement.
  • Encapsulation in Java provides an option of code-reusability.
  • Using encapsulation will help in making changes to an existing code quickly.
  • Unit testing a code designed using encapsulation is elementary.
  • Standard IDEs have the support of getters and setters; this makes coding even faster. 

So with this, we have now arrived at the end of the 'Encapsulation in Java' article. We hope you enjoyed learning about the essential concepts of encapsulation in Java.

Are you interested in learning more about the Java Programming Language and getting certified as a professional Java Developer? Then, check out our Java training and certification program curated by the most experienced real-time industry experts.

Got a question about the "Encapsulation in Java” process? Please mention it in the comment section of this article, and we'll have our experts answer it for you right away.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.