DEV Community

loading...

JAVA: OOP (Encapsulation)

supriya2371997 profile image Supriya Kolhe ・4 min read

Q1: Access Modifiers?
Q2: OOPS: Encapsulation?
Q3: How to achieve Encapsulation?
Q4: Advantages?

1. Access Modifier:-

1.1 used to set the access level for classes, attributes, methods and constructors.
1.2 java provides access modifiers such as public, private, default and protected.

Class level access modifiers:-

public: class is accessible by other classes
default: class can be accessed by the classes from the same package. by default, the access modifier is set to default

Method, attributes, constructor level access modifiers:-

public: code is accessible for all classes
private: code is only accessible within the class
default: code is only accessible in the same package. by default, the access modifier is set to default
protected: code is accessible in the same package and outside the package through subclass

Alt Text

2. Encapsulation:-

2.1 it is one of the most powerful and fundamental concepts of Object-Oriented Programming
2.2 also called as data-hiding since it hides sensitive data from the user in order to keep it safe from outside interference and misuse

wrapping up of data into a single unit in order to prevent access to it from the outside world

2.3 for example, a car has many parts such as wheels, engine etc which together form a single object.

2.4 since there is a prohibition for direct access to the data. Functions are the only ways to access data. It basically creates a shield due to which code or data can't be accessed outside the shield

in order to read the data items, you call the member function in the object. It will read the data items from the function and return the value to you. The data can't be accessed directly in order to keep it protected and accidental alteration.

Alt Text
2.5 For example, In a large organisation there are many departments like sales, payroll, accounts, etc. And each department had it's own staff to maintain it's data.

  • Suppose an employee wants to know the data of import and export material, but he won't be allowed to access that data. Rather he will have to issue an order from the authorised person requesting for the required information. Then also the particular employee will not be directly accessing the data, rather the employees of the authorised department will gather the request data and pass it to the desired employee. >therefore, we can say that here Department is a single unit which is holding data and employees together.

3. Achieve Encapsulation:-

step 1: declare class level variables private

so they can't be accessed from outside of the class

step 2: provide setter and getter methods that are declared as public

used to view or change the value of the variable

Github link to below code
Simple example of encapsulation:-

public class Encapsulation
{
  //Declaring variables as private
  private String EmployeeName;
  private String EmployeeJob;
  private int EmployeeAge;

  //'public' Setter and Getter Methods
  public void setName(String EmployeeName)
  {
    this.EmployeeName = EmployeeName;
  }
  public String getName()
  {
    return EmployeeName;
  }

  public void setJob(String EmployeeJob)
  {
    this.EmployeeJob = EmployeeJob;
  }
  public String getJob()
  {
    return EmployeeJob;
  }

  public void setAge(int EmployeeAge)
  {
    this.EmployeeAge = EmployeeAge;
  }
  public int getAge()
  {
    return EmployeeAge;
  }

  public static void main (String[] args)
  {
    //creating object
    Encapsulation e = new Encapsulation();

    // setting values through setter methods
    e.setName("SUPRIYA KOLHE");
    e.setAge(22);
    e.setJob("SOFTWARE DEVELOPER");

    // Displaying values through Getter methods
    System.out.println("Name of the employee: " + e.getName());
    System.out.println("Age of the employee: " + e.getAge());
    System.out.println("Job of the employee: " + e.getJob());
  }//PSVM ends
}//Encapsulation class ends

/*
Name of the employee: SUPRIYA KOLHE
Age of the employee: 22
Job of the employee: SOFTWARE DEVELOPER
*/
Enter fullscreen mode Exit fullscreen mode

3.1 as you can see in the above example, here we are setting values using Setter methods and accessing them using Getter method

where Setter methods set the value of the variable and Getter methods returns the values

4. Advantages:-

Alt Text

Data Hiding:- programmer is able to hide inner classes and provide authorization to access them to only desired users.

limits the accessibility of data

Easy to Testing:- because of sorted structure, it is easy to perform unit testing.
Flexibility and Maintainability:- programmer can make code read-only and write-only according to need.

Getter methods are used to read/fetch the data. Thus using only them will make code read-only.
Setter methods are used to write into the variables. Thus using only them will make code write-only.

Read-only example:

public class Employee
{  
    private String Dept="IT";  
    public String getDept()
    {  
         return Dept;   
    }  
}  
Enter fullscreen mode Exit fullscreen mode

Write-only example:

public class Employee
{  
    private String Salary="80000";  
    public String setSalary()
    {  
         return Salary;   
    }  
}  
Enter fullscreen mode Exit fullscreen mode

Getter and Setter Methods:- able to access private variables outside the class
Code Reusability:- able to use existing code effectively again and again.
Control & security:- a class has control over the variables and fields i.e. what is stored in them.

Please comment if you have any feedback or suggestions

Discussion (0)

Forem Open with the Forem app