Encapsulation in Java with Example
Updated on Nov 14, 2022 | 7 min read | 6.4k views
Share:
For working professionals
For fresh graduates
More
Updated on Nov 14, 2022 | 7 min read | 6.4k views
Share:
Table of Contents
Encapsulation in Java is the process of wrapping up data or all the variables and the code that’s acting on that data into one single unit or bundle. It can only be accessed through a member function that belongs to an individual class of its own in which it has been declared. This mechanism binds the code together with the data it manages. It also helps prevent the accessing of data by any code outside the shield.
Encapsulation in Java is also known as a combination of data hiding and abstraction. The data in a particular class is hidden and abstracted from other classes. This goal is attained by making the members or variables of a class private. This process can be accomplished by declaring all the members and variables privately and writing public methods in the class. This results in “getting” and “setting” the value of each variable privately.
Check out our free courses to get an edge over the competition
In Java, there are mainly two steps to implement data encapsulation:
Here is a code depicting encapsulation in Java:
class EncapsulationDemo{
private int ssn;
private String cusName;
private int cusAge;
//Getter and Setter methods
public int getCusSSN(){
return ssn;
}
public String getCusName(){
return cusName;
}
public int getcusAge(){
returncusAge;
}
public void setcusAge(int newValue){
CusAge = newValue;
}
public void setcusName(String newValue){
CusName = newValue;
}
public void setcusSSN(int newValue){
ssn = newValue;
}
}
public class EncapsTest{
public static void main(String args[]){
EncapsulationDemo obj = new EncapsulationDemo();
obj.setcusName(“Erica”);
obj.setcusAge(24);
obj.setcusSSN(001122);
System.out.println(“Customer Name: ” + obj.getCusName());
System.out.println(“Customer SSN: ” + obj.getCusSSN());
System.out.println(“Customer Age: ” + obj.getCusAge());
}
}
Check out upGrad’s Java Bootcamp
Output:
Customer Name: Erica
Customer SSN: 001122
Customer Age: 24
Typically, there are three types of encapsulations:
The setters and getters functions in Java are used to modify or retrieve the value of a data member. This process is essentially known as member variable encapsulation.
Check out upGrad’s Full Stack Development Bootcamp (JS/MERN)
Any function used for the internal implementation of an API must be declared private at all times. Any method can easily be made public since the user provides the data. Programmers should always keep a check that all functions that mustn’t be public are hidden.
Almost the same principles of function encapsulation apply to class encapsulations. The classes in the program should never be a part of any public interface.
Encapsulation and the function of data hiding are often used interchangeably by programmers. As already mentioned above, Java encapsulation majorly deals with binding data into a single unit to maintain security and ensure more effective management. Generally, data hiding acts by restricting users to avail data member access with the help of hiding all implementation details.
Java mainly provides the following four access modifiers:
When each variable is declared private in a particular class, it is commonly termed a “tightly encapsulated class” in Java. In such a case, programmers must check whether the concerned class contains the getter or setter methods and whether any of those methods are declared to be public or not.
It is important to note that when the parent class is not tightly encapsulated, the child class is automatically not tightly encapsulated. This is because the parent class’s non-private data members are by default available to every child class. Hence, we can easily conclude that data hiding, encapsulation, and tightly encapsulated class concepts are highly effective in maintaining the optimum level of security.
Following are the several reasons which make encapsulation in Java so important:
”
upGrad’s Exclusive Software Development Webinar for you –
SAAS Business – What is So Different?
Here are the top advantages of Encapsulation in Java:
Check out upGrad’s 5-month online Job-linked PG Certification in Software Engineering to begin your career in software development. The course comprises five hands-on projects and a specialization in MERN/Cloud-Native to help build a competitive portfolio and land lucrative job opportunities.
If you are a final year student or graduate with up to 2 years of experience, the Job-linked PG Certification in Software Engineering course can be your entry-point into the software domain.
Get in touch with us today to book your seat!
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources