Tutorial Playlist

Java Tutorial for Beginners


10 Reasons That Explain Why You Need to Learn Java

Lesson - 1

What is Java: A Beginners Guide To Java

Lesson - 2

What Is JDK? Why Do We Need It?

Lesson - 3

One-Stop Solution for Java Installation in Windows

Lesson - 4

How to Get Started With Eclipse IDE?

Lesson - 5

What Are Java Strings And How to Implement Them?

Lesson - 6

Arrays In Java: Declare, Define, and Access Array

Lesson - 7

What Are Java Collections and How to Implement Them?

Lesson - 8

What Are Java Classes and Objects and How Do You Implement Them?

Lesson - 9

How to Implement the Revolutionary OOPs Concepts in Java

Lesson - 10

What is Encapsulation in Java and How to Implement It?

Lesson - 11

What is an Abstract Class in Java and How to Implement It?

Lesson - 12

What is Inheritance in Java and How to Implement It

Lesson - 13

What is Java Interface and Why it's Needed?

Lesson - 14

What is Polymorphism in Java and How to Implement It?

Lesson - 15

What is a Java Lambda Expression and How to Implement It?

Lesson - 16

Your One-Stop Solution for Multithreading in Java

Lesson - 17

The Differences Between C++ and Java That You Need To Know

Lesson - 18

Java vs. Python: Which is the Best Programming Language?

Lesson - 19

Java vs JavaScript: Know The 8 Major Differences

Lesson - 20

Top 25 Pattern Programs in Java For Printing Numbers

Lesson - 21

Java EE Tutorial: All You Need To Know About Java EE

Lesson - 22

What is Exception Handling in Java?

Lesson - 23

What Is Java JDBC? The Complete Reference

Lesson - 24

What is Java API and The Need for Java APIs?

Lesson - 25

Introduction To Java Servlets and Its Life-Cycle

Lesson - 26

10 Best Java Frameworks You Should Know in 2021

Lesson - 27

Top 10 Brilliant Java Project Ideas You Need to Know

Lesson - 28

Top 70 Java Interview Questions and Answers for 2022

Lesson - 29

Java Programming: The Complete Reference You Need

Lesson - 30

All You Need to Know to Implement JUnit Testing in Java

Lesson - 31

What Is Junit? a Look Into the Best Java Testing Framework

Lesson - 32

Ruby on Rails

Lesson - 33
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.

What is Encapsulation in 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.


<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.


package dc;

public class c 


public static void main (String[] args) 


Employee e = new Employee(); 




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) {




Employee's name: Robert

Employee's age: 33

Employee's ID: 1253

Full Stack Java Developer Course

In Partnership with HIRIST and HackerEarthEXPLORE 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.


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.


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


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 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.


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


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;





I have a vehicle.

It has four tires.


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


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();


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




Student rank is 1022


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.


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";





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


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


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


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();







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


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.