Recent comments

Breaking Post

Encapsulation in Java

Encapsulation in Java
Encapsulation in Java
Encapsulation in Java

Encapsulation is defined as the wrapping of data under a unit. This is a mechanism that binds the code together and the data it manipulates. One way to think about encapsulation is to have a protective cover that prevents data from accessing the code outside this shield. Therefore, it is also known as Data Hyde.

Encapsulation is one of four fundamental OOP concepts. The other three are polymorphism, abstraction and inheritance.



Encapsulation in Java
1. In technically Encapsulation, the variables or data of a class are hidden from any other category and can only be accessed through any member function of that class in which they are declared.

2. As in Encapsulation, the data of a class is hidden from other sections, so it is also known as data-hiding.

3. Encapsulation can be obtained: - To declare all the variables in the class as private and to write and publicize the class in order to determine and obtain the values of variables.

// Java program to displayate encapsulation
public class Encapsulate

{
    // private variables declared
    // this 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 geek
    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 geek
    public void setName (String newName)
    {
      geekName = newName;
    }
      
    // set method for roll to access
    // private variable geekRoll
    public void setRoll (int newRoll)
    {
      geekRoll = newRoll;
    }
}


Class EncapsulateDemo is encoded in the above program because the variable is declared private. Methods such as getAge (), getName (), getRoll () are set as public, these methods are used to access these variables. Setter methods such as SetName (), setAge (), setRoll () are also declared public and used to set the parameters for the variable.

The program to reach the range of the Class EncapsulateDemo is shown below:
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 to geekRoll is not possible
        // due to encapsulation
        // System.out.println ("Geek's roll:" + obj.geekName);
    }
}


Benefits of Encapsulation:

1. Hiding Data: - The user will not have any idea about the internal implementation of the class. This user will not be able to see how the values are stored in the square variable. He only knows that we are passing values in a setter method and the variables are being initialized with that value.

2. Increased flexibility: - On the basis of our requirement, we can create class variables only for reading or writing. If we want to make the variables to be read-only, then we have to leave the set man name (eg, set) from the above program, or if we want to create the variable as writing only then we have to leave. Get methods such as getName (), getAge () etc. from above program
CA

3. Reusable: - Encapsulation also improves reusability and is easy to replace with new requirements.

4. The test code is simple: - It is easy to test the encapsulated code for unit testing.


 
 NOTE- The word decomposition refers to the process of removing headers and trailers because the data passes from the bottom to the upper layers. This process is on a computer that receives data.

No comments