One of the four essential OOPS concepts is encapsulation. The remaining three are abstraction, polymorphism, and inheritance. In Java, encapsulation is a way of combining the code that manipulates the data and the data itself into a single unit. Encapsulation means that a class's variables are kept private from other classes and are only accessible through its own methods. As a result, it is sometimes referred to as data concealing. In this article, we will be talking about data hiding in Java, and the need and advantages for encapsulation in Java.
Java encapsulation is a process where a class's variables or data are concealed from all other classes and are only accessible through member functions of the class in which they were declared. It is also known as a combination of data-hiding and abstraction because, similar to encapsulation, the data in a class is hidden from other classes using the data hiding concept, which is accomplished by making the members or methods of a class private, and the class is exposed to the end-user or the world without providing any details behind implementation.
Encapsulation is accomplished by writing public functions in the class for setting and getting the values of the variables and declaring all of the class's variables as private.
With the setter and getter methods, it is more clearly described. Encapsulation is majorly of 3 types:
Let us see a simple Java code and understand the concept of encapsulation better:
class Area_Rectangle {
int len;
int br;
Area_Rectangle(int len, int br) {
this.len = len;
this.br = br;
}
public void getArea() {
int area = len * br;
System.out.println("Area of the rectangle is: " + area);
}
}
class Main {
public static void main(String[] args) {
Area_Rectangle rectangle = new Area_Rectangle(4, 16);
rectangle.getArea();
}
}
Output:
Area of the rectangle is: 64
Now, let us see another Java code for encapsulation where the program demonstrated how to access the variables of the class.
class Encapsulation {
private int age;
public int getAge() { return age; }
public void setAge(int age)
{
this.age = age;
}
}
class ABC {
public static void main(String[] args)
{
Encapsulation n1 = new Encapsulation();
n1.setAge(25);
System.out.println("The age is: " + n1.getAge());
}
}
Output:
The age is: 25
Wish to make a career in the world of Java? Start with HKR'S Java Training !
Let us discuss the need for encapsulation in Java:
If you have any doubts on Java, then get them clarified from Java Industry experts on our Java Community !
Top 30 frequently asked JAVA Interview Questions !
A process called as data concealing is employed to restrict access to the data methods, data members, and the logical implementation. Utilising access specifiers will enable data concealing. The following are the four access specifiers that we have :
package P;
public class X
{
public void display()
{
System.out.println("Welcome to HKR Trainings");
}
}
The output of the Java code is:
Welcome o HKR Trainings
package P;
public class X
{
protected void display()
{
System.out.println("Welcome o HKR Trainings");
}
}
package P;
class X
{
private void display()
{
System.out.println("Welcome to HKR Trainings);
}
}
class Y
{
public static void main(String args[])
{
X obj = new X();
obj.display();
}
}
The output will be:
.java
error: invalid flag: /tmp/iREEX2TaoL/Y
Usage: javac
use --help for a list of possible options
dash: 2: .java: not found
Want to know more about Java,visit here Java Tutorial !
Conclusion
In this article, we have discussed Java encapsulation in depth. Java encapsulation is a process where a class's variables or data are concealed from all other classes and are only accessible through member functions of the class in which they were declared. It is also known as a combination of data-hiding and abstraction. We have also discussed the need and advantages of java encapsulation along with the data hiding in java using private, public, default and protected.
Related Article :
Batch starts on 6th Jun 2023, Weekday batch
Batch starts on 10th Jun 2023, Weekend batch
Batch starts on 14th Jun 2023, Weekday batch
Encapsulation is a process where a class's variables or data are concealed from all other classes and are only accessible through member functions of the class in which they were declared.
The three types of encapsulation are member variable encapsulation, class encapsulation and function encapsulation.
A structured data object's values or state are buried inside a class using encapsulation to prevent direct client access that can reveal undocumented implementation details or compromise state invariance upheld by the methods.
When a lower-layer protocol gets data from a high-layer protocol, it encapsulates the data and inserts it into the data component of its frame.
We can ourselves define the encapsulation as public or private.