in , ,

Java Encapsulation

Java Encapsulation
Java Encapsulation

Contents

Java Encapsulation

In this tutorial, you will find out about encapsulation and data hiding in Java with the help of examples.

Java Encapsulation

Encapsulation is one of the key highlights of object-oriented programming. Encapsulation refers to the packaging of fields and methods inside a single class.

It keeps external classes from getting to and changing fields and methods for a class. This likewise helps with accomplishing data hiding.


Example 1: Java Encapsulation

class Area {

  // fields to calculate area
  int length;
  int breadth;

  // constructor to initialize values
  Area(int length, int breadth) {
    this.length = length;
    this.breadth = breadth;
  }

  // method to calculate area
  public void getArea() {
    int area = length * breadth;
    System.out.println("Area: " + area);
  }
}

class Main {
  public static void main(String[] args) {

    // create object of Area
    // pass value of length and breadth
    Area rectangle = new Area(5, 6);
    rectangle.getArea();
  }
}

Output

Area: 30

In the above example, we have made a class named Area. The main purpose of this class is to calculate the area.

To calculate a zone, we need two variables: length and breadth and a method: getArea(). Subsequently, we packaged these fields and techniques inside a single class.

Here, the fields and methods can be accessed from different classes also. Henceforth, this isn’t data hiding.

This is just encapsulation. We are simply keeping comparable codes together.

Note: People often consider encapsulation as data hiding, however that is not completely true.

Encapsulation. refers to the packaging of related fields and methods together. This can be used to accomplish data hiding. Encapsulation in itself isn’t data hiding.


Why Encapsulation?

  • In Java, Encapsulation helps us to keep related fields and methods together, which makes our code cleaner and simple to peruse.
  • It helps with controlling the values of our data fields. For instance,
class Person {
  private int age;

  public void setAge(int age) {
    if (age >= 0) {
      this.age = age;
    }
  }
}

Here, we are making the age variable private and applying logic inside the setAge() method. Now, age cannot be negative.
The getter and setter methods provide read-only or write-only access to our class fields. For example,

getName()  // provides read-only access
setName() // provides write-only access
  • It helps with decoupling parts of a system. For instance, we can encapsulate code into numerous bundles.

These decoupled segments (bundle) can be created, tried, and fixed freely and simultaneously. Furthermore, any adjustments in a specific segment don’t have any impact on different parts.

  • We can likewise accomplish data hiding using encapsulation. In the above example, if we change the length and expansiveness variable into private, at that point the admittance to these fields is limited.

Also, they are kept escaped external classes. This is called data hiding.


Data Hiding

Data hiding is a way of restricting the access of our data members by hiding the implementation details. Encapsulation additionally gives an approach to data hiding.

We can use access modifiers to achieve data hiding. For example,

Example 2: Data hiding using the private specifier

class Person {

  // private field
  private int age;

  // getter method
  public int getAge() {
    return age;
  }

  // setter method
  public void setAge(int age) {
    this.age = age;
  }
}

class Main {
  public static void main(String[] args) {

    // create an object of Person
    Person p1 = new Person();

    // change age using setter
    p1.setAge(24);

    // access age using getter
    System.out.println("My age is " + p1.getAge());
  }
}

Output

My age is 24

In the above example, we have a private field age. Since it is private, it cannot be accessed from outside the class.

In order to access age, we have used public methods: getAge() and setAge(). These methods are called getter and setter methods.

Making age private allowed us to restrict unauthorized access from outside the class. This is data hiding.

If we try to access the age field from the Main class, we will get an error.

// error: age has private access in Person
p1.age = 24;

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Your email address will not be published. Required fields are marked *

Java Polymorphism

Java Polymorphism

Java Nested and Inner Class

Java Nested and Inner Class