What is Encapsulation?

What is Encapsulation?

Asked on January 18, 2019 in Java.
Add Comment


  • 4 Answer(s)
    • Protects the code from others.
    • Code maintainability.

    Example:

    We are declaring ‘a’ as an integer variable and it should not be negative.

    1 public class Addition(){
    2 int a=5;
    3 }

    If someone changes the exact variable as “a = -5” then it is bad.

    Answered on January 18, 2019.
    Add Comment

    Encapsulation means hiding the data. In other words a class only exposes those properties or information which is authorized to be seen. Consider the below exapmle where a certain property called ConfidentialMessage is accesible only to the Admin role. This property is made private and is returned through another method which checks the role of an user and return the message only for admin.

    public class Message
    {
    public Message()
    {
    ConfidentialMessage = “I am Nemo”;
    }
    private string ConfidentialMessage { get; set; }
    public string GetMessage(string name)
    {
    string message = string.Empty;
    if (name == “Admin”)
    {
    message = this.ConfidentialMessage;
    }
    return message;
    }
    }

    Answered on January 25, 2019.
    Add Comment

    Encapsulation in Java

    Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates.Other way to think about encapsulation is, it is a protective shield that prevents the data from being accessed by the code outside this shield.

    • Technically in encapsulation, the variables or data of a class is hidden from any other class and can be accessed only through any member function of own class in which they are declared.
    • As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-hiding.
    • Encapsulation can be achieved by: Declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables.

    To achieve encapsulation in Java −

    • Declare the variables of a class as private.
    • Provide public setter and getter methods to modify and view the variables values.

     

    // Java program to demonstrate encapsulation
    public class Encapsulate
    {
        // private variables declared 
        // these can only be accessed by 
        // public methods of class
        private String Name;
        private int Roll;
        private int Age;
     
        // get method for age to access 
        // private variable Age
        public int Age() 
        {
          return Age;
        }
      
        // get method for name to access 
        // private variable Name
        public String Name() 
        {
          return Name;
        }
         
        // get method for roll to access 
        // private variable Roll
        public int Roll() 
        {
           return Roll;
        }
      
        // set method for age to access 
        // private variable age
        public void Age( int newAge)
        {
          Age = newAge;
        }
      
        // set method for name to access 
        // private variable Name
        public void setName(String newName)
        {
          Name = newName;
        }
         
        // set method for roll to access 
        // private variable Roll
        public void setRoll( int newRoll) 
        {
          Roll = newRoll;
        }
    }
    Answered on February 3, 2019.
    Add Comment

    Encapsulation in Java
    Encapsulation in Java is a process of wrapping code and data together into a single unit, for example, a capsule which is mixed of several medicines.

    encapsulation in java
    We can create a fully encapsulated class in Java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it.

    The Java Bean class is the example of a fully encapsulated class.

    Advantage of Encapsulation in Java
    By providing only a setter or getter method, you can make the class read-only or write-only. In other words, you can skip the getter or setter methods.

    It provides you the control over the data. Suppose you want to set the value of id which should be greater than 100 only, you can write the logic inside the setter method. You can write the logic not to store the negative numbers in the setter methods.

    It is a way to achieve data hiding in Java because other class will not be able to access the data through the private data members.

    The encapsulate class is easy to test. So, it is better for unit testing.

    The standard IDE’s are providing the facility to generate the getters and setters. So, it is easy and fast to create an encapsulated class in Java.

    //A Java class to test the encapsulated class.
    package com.javatpoint;
    class Test{
    public static void main(String[] args){
    //creating instance of the encapsulated class
    Student s=new Student();
    //setting value in the name member
    s.setName(“vijay”);
    //getting value of the name member
    System.out.println(s.getName());
    }
    }
    Compile By: javac -d . Test.java
    Run By: java com.javatpoint.Test

    Output:

    vijay

    Answered on February 4, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.