Home » Java Tutorial » Encapsulation in Java with Examples

Encapsulation in Java with Examples

Encapsulation in java is the mechanism that binds together data member and member functions and keeps both safes from outside interference. One way to think about encapsulation is as a protective wrapper that prevents the code and data from being arbitrarily accessed by other codes defined outside the wrapper. The access to the code and data inside the wrapper is tightly controlled through a well-defined interface.

Table of Contents

Why do we need encapsulation in java?

The other meaning of encapsulation means data hiding. In most cases, the data stored in data members are sensitive and can easily be accessed. 1With the help of encapsulation, we can hide the data using access specifiers. Creating a class with private data member and member function will not allow the outsider class to access it. The members and methods are accessible within a class only. Ultimately, the security of data is increased.

How to achieve encapsulation in java?

There are two steps to achieve encapsulation in java.

  1. Make all the data members declared within a class private. So no one can access it from outside.
  2. Provide a getter and setter method in the class as public to set the values to data members and access it.

Example of encapsulation in java.

The encapsulation example is given below. In the below example, we have set the length and breadth from main(). In the class, we have calculated the area and return it.


import java.util.Scanner;

public class encapsulation {

    private int length; //declaring private members
    private int breadth;
    public void setlength(int length)
        this.length = length; //setter method for length
    public void setbreadth(int breadth)
        this.breadth = breadth; //setter method for breadth
    public int getlength()
        return length; //getter method for length to access the value
    public int getbreadth()
        return breadth; //getter method for breadth to access the value
    public int area() //area function to calculate area
        int area;
        area = length*breadth;
        return area;
    public static void main(String[] args) {
        int len=0;
        int bre=0;
        Scanner sc = new Scanner(System.in); //Scanner class to accept data from user
        System.out.println("Enter length of rectangle"); //accepting data from user
        len = sc.nextInt();
        System.out.println("Enter breadth of rectangle"); //accepting data from user
        bre = sc.nextInt();
        encapsulation en = new encapsulation(); //creating object of encapsulation class
        en.setlength(len); //setting length to the data member
        en.setbreadth(bre); //setting breadth to the data member
        System.out.println("The length of rectangle is "+en.getlength()); //accessing the value of length
        System.out.println("The breadth of rectangle is "+en.getbreadth()); //accessing the value of breadth
        System.out.println("Area of rectangle is "+en.area()); //call to the area function


Enter length of rectangle
Enter breadth of rectangle
The length of rectangle is 5
The breadth of rectangle is 6
Area of rectangle is 30
Do you know?
1. Method overloading in java
2. Prime Number Program in Java
3. Type Casting In Java
4. Ternary Operator in Java
5. Immutable Class in Java
6 . Printf Java
7. While Loop Java
8. StringBuilder java
9. Binary Search Java
10. Encapsulation in java

Pin It on Pinterest