Encapsulation Definition: Encapsulation is one of the pillar concepts of Object-oriented programming. The Java Encapsulation mechanism does wrapping the data (variables) and methods together in a single unit. Where the variables of a class are hidden from other classes and can be accessed only through the methods (Getter – Setter) of their current class.
This is the Java encapsulation definition, now in the tutorial, you will learn how to create it and use it with examples.
How to do Encapsulation in Java?
Here are a few steps to achieve Encapsulation, First need to create one class then follow the steps.
- Declare all variables as private (Access Modifiers) in class.
- Create public setter-getter methods to access and update the value of Private Variables.
Java encapsulation example
Here is a very simple example of Java encapsulation example code and program.
Create a Student.java, with a private “name” variable. And Generate Getter and Setter methods for it. If you don’t know how to create Getter and Setter methods, then look at the tutorial the help section mentioned all steps with screenshots.
public class Student { //private data member private String name; //getter method for name public String getName() { return name; } //setter method for name public void setName(String name) { this.name = name; } }
Now Another class Hello.java will access this variable through methods as below example.
class Hello { public static void main(String[] args) { // Creating instance of the encapsulated student class Student s = new Student(); // Setting value s.setName("John"); // Getting System.out.println(s.getName()); } }
Output: John
Read-Only class
You can only read the variables in this code. For that Generate only getter methods for variables.
public class Student { //private data member private String name; //getter method for name public String getName() { return name; } }
Write-Only class
You can only Write the variables in this code. For that Generate only Setter methods for variables.
public class Student { //private data member private String name; //setter method for name public void setName(String name) { this.name = name; } }
What are the Advantages of encapsulation in Java?
There is serval advantage of using encapsulation concept in code, let’s see it.
- You can make the class read-only or write-only by the setter or getter method. It means your class variable can have only read or write permission by choice of using the method. You can skip the getter or setter methods.
- Control Over Data – Can control on store data, if you want a positive integer only then can write the logic in the method.
- Data Hiding – By declaring a Variable (data) private will hide for other classes.
- Easy Testing – Can do easily unit testing.
- Flexible: You can change one part of the code without affecting other parts of coding.
Last but not advantage, the IDE facility generates getters and setters. it’s very helpful in creating getters and setters in Java or other programming languages.
Encapsulation in oops is very useful as you see the above example an advantage, it’s not only in java programming. Other programming languages which also based on object-oriented programming concepts have these advantages.
Q: How to generate getter and setter in java?
It’s easy to generate getter and setter using an IDE, here are the steps to do it.
1 Step: Right-click on a variable, pop will open then click on Generate…
2 Step: Another pop will open, select Getter and Setter and click on it.
3 Step: Last step select variables, which do you want to create Getter and Setter, and click ok, all methods will generate in your class.
With the same step 2, you can generate only Getter or setter. Do comment if any doubt suggestion on Encapsulation.
Do comment if you have any questions and suggestions on this tutorial.
Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1Java version 11
All Java Encapsulation Examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.