uylmz
0
Q:

encapsulation

In object-oriented programming, encapsulation refers to the bundling 
of data with the methods that operate on that data, or the restricting
of direct access to some of an object's components
5
Encapsulation in Java is a mechanism to wrap up variables and methods together
as a single unit. It is the process of hiding information details and protecting
data and behavior of the object. It is one of the four important OOP concepts. 
 The encapsulate class is easy to test, so it is also better for unit testing.
1
we can hide direct access to the data by giving private keyword. 
And access the data using public getter and setter methods.

• Getter method is return type(you must initialize to some variable like string), 
no parameters, read data.
• Setter method is non-return method with void, must have parameter,
modify the data, 
use this keyword if param has same name as private data. 

Data type of the param must be same as private data. 
If private data is final we cannot modify.
Ex:In my project i created multiple POJO/BEAN classes
(Json to java (Gson, Jakson )) in order to manage test data and actual data. 
EX: I take JSON from API response and convert to object of my POJO 
class all variables are private with getters and setter. 
Also in order to store credentials or sensitive data in my framework 
I have use encapsulation, configuration reader also known as property file or 
excel sheet to hide data from outside world to get access.
I use Apache POI if the data store in Excel in order to extract/read and 
modify data.
1
// Java program to demonstrate encapsulation 
public class Encapsulate 
{ 
    // private variables declared  
    // these can only be accessed by  
    // public methods of class 
    private String geekName; 
    private int geekRoll; 
    private int geekAge; 
  
    // get method for age to access  
    // private variable geekAge 
    public int getAge() { 
      return geekAge; 
    } 
   
    // get method for name to access  
    // private variable geekName 
    public String getName() { 
      return geekName; 
    } 
      
    // get method for roll to access  
    // private variable geekRoll 
    public int getRoll() { 
       return geekRoll; 
    } 
   
    // set method for age to access  
    // private variable geekage 
    public void setAge( int newAge) { 
      geekAge = newAge; 
    } 
   
    // set method for name to access  
    // private variable geekName 
    public void setName(String newName) { 
      geekName = newName; 
    } 
      
    // set method for roll to access  
    // private variable geekRoll 
    public void setRoll( int newRoll) { 
      geekRoll = newRoll; 
    } 
} 

public class TestEncapsulation {     
    public static void main (String[] args) { 
        Encapsulate obj = new Encapsulate(); 
          
        // setting values of the variables  
        obj.setName("Harsh"); 
        obj.setAge(19); 
        obj.setRoll(51); 
          
        // Displaying values of the variables 
        System.out.println("Geek's name: " + obj.getName()); 
        System.out.println("Geek's age: " + obj.getAge()); 
        System.out.println("Geek's roll: " + obj.getRoll()); 
          
        // Direct access of geekRoll is not possible 
        // due to encapsulation 
        // System.out.println("Geek's roll: " + obj.geekName);         
    } 
} 
2
#include<iostream>
using namespace std;
class ExampleEncap{
private:
   /* Since we have marked these data members private,
    * any entity outside this class cannot access these
    * data members directly, they have to use getter and
    * setter functions.
    */
   int num;
   char ch;
public:
   /* Getter functions to get the value of data members.
    * Since these functions are public, they can be accessed
    * outside the class, thus provide the access to data members
    * through them
    */
   int getNum() const {
      return num;
   }
   char getCh() const {
      return ch;
   }
   /* Setter functions, they are called for assigning the values
    * to the private data members.
    */
   void setNum(int num) {
      this->num = num;
   }
   void setCh(char ch) {
      this->ch = ch;
   }
};
int main(){
   ExampleEncap obj;
   obj.setNum(100);
   obj.setCh('A');
   cout<<obj.getNum()<<endl;
   cout<<obj.getCh()<<endl;
   return 0;
}
0

New to Communities?

Join the community