Encapsulation In Java With Realtime Example

Introduction to Encapsulation in Java: In this tutorial, I am going to discuss about the "real-time example of encapsulation in java" hope it will helpful for you.

Encapsulation: [A mechanism which is to hide information (Data or Implementation)] Here, the capsule means it is just a container or we can say that a structure that enclose a body part (function or class).

Encapsulation in java with realtime example
Img: Wikipedia

Encapsulation is a process of binding data member (like variables, properties) and member function (like methods) into a single unit. If we talk about class and function it is the best example of encapsulation in java.

In other words, encapsulation is the process of hiding the internal details of an object. We can achieve encapsulation by hiding details using the access modifier (Public, Private, Protected, Default).

class Students          //No matter there is union or struct
public double totalMarks;  //data, variable or attribute
   public double MarksScored; 

   Public double GetPercentageMarks()      //operation
       double Percentage =(MarksScored /totalMarks) * 100;
       return Percentage;


Students OOpsStd = new Students(); 

In the above code we have encapsulated totalMarks, MarksScored and Method GetPercentMarks. While creating a "OOpsStd" object, the implementation of  GetPercentMarks will not be shown.


A function is also Example of Encapsulation

void add()
    int a = 4;
    int b = 5;      //data, variable or attribute
    int  c;

    c =  a + b;       //operation

    cout << "Addtion of 2 no is: " << c;



Why we use Encapsulation in java

1) We can control the way of data accessibility.
2) Later on, you can modify the code at your end.
3) Encapsulation in java helps us to achieve loosely couple.
4) The main goal of Encapsulation is to achieve simplicity in our application.
5) Encapsulation in Java allows you to change one part of the code without affecting another part line of code.

Encapsulation in java with real-time example:

Encapsulation in java with realtime example

Encapsulation play a major role by developing the application the literal meaning of abstraction or the simplest mean of show the operational part or providing the operational part to the use is abstraction and encapsulation is hiding the complexity from the user is set to be the encapsulation 


We can say if there is a encapsulation there will we abstraction if we place an order for mobile to X company customer or  user is only consort for operation the mobile like brightness , volume ,SMS,call,power off he is not caller of the internal component of the mobile like electrical part of handset, how touch screen made he doesn't know that's encapsulation. He only knows functioning part of mobile or operational part of mobile this call abstraction.

Next Post »


Click here for comments
June 28, 2018 at 11:57 AM ×

Informative post

Congrats bro Nisha you got PERTAMAX...! hehehehe...