• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Java Program
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » Encapsulation In Java

Encapsulation In Java

Last Updated on: August 15, 2020 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • What is Encapsulation in Java?
  • Advantages of Encapsulation

Encapsulation In Java: Like in our every post of SoftwareTestingo blog in this post also we are going to learn something important concept of Java Programming Language. As you know that, for a better programmer, you have a clear idea about different Java OOPS concepts topics like Inheritance, Abstraction, Encapsulation, and Polymorphism. So in this post, we are going to discuss the Encapsulation In Java.

In this article, we are going to share one of the most important concepts of OOPS that are Encapsulation in Java, and also we are going to learn how we can achieve that. The Process of binding data members and methods in a single unit is called encapsulation in Java. During this post, we are going to discuss:

  • What is Encapsulation
  • Why we need Encapsulation in Java?
  • Benefits of Encapsulation
  • Encapsulation Example

What is Encapsulation in Java?

Encapsulation refers to the binding of the data member and member function (methods) in a single unit. In other words, we can say that Encapsulation is made a protective shield by which it prevents the data from the outer side of the shield.

Because of Encapsulation, the variables and data members (methods) of a class is hidden from other classes and can be accessed only through the member function of the same class where they are declared. Therefore, it is also known as data hiding.

Encapsulation In Java With Explanation
Encapsulation In Java With Explanation

Important Points of Encapsulation:

  • In Encapsulation, the data members (Variables) and Methods (functions) of a class are hidden from the classes but can be accessed only through the member functions of own class in which they are declared.
  • The data of a class is hidden from other classes, so it is also known as data-hiding.
  • We can achieve the encapsulation by declaring the variables as private and writing public methods to set and get the values of the variables.

Example Of Encapsulation:

  • When we are writing a java program, that itself is an example of encapsulation. Because when we write a class that binds the variables and methods in a single unit and hides the complexity of class from other classes.
  • Another Good example of encapsulation is a capsule because a capsule also encapsulate different medicine under a capsule

Program 1: (Base Class)

// Java program to demonstrate encapsulation
public class Encapsulate
{
   // private variables declared
   // these 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 geekName
   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 geekName
   public void setName(String newName)
   {
      geekName = newName;
   }

   // set method for roll to access
   // private variable geekRoll
   public void setRoll( int newRoll)
   {
      geekRoll = newRoll;
   }
}

Extended Class: Child Class

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 of geekRoll is not possible
      // due to encapsulation
      // System.out.println("Geek's roll: " + obj.geekName);
   }
}

Advantages of Encapsulation

Data Hiding: During this process, the user doesn’t have any idea about the inner implementation of the class, like how the values are stored inside a variable. The user only aware that they are passing a value to a setter method, and the value is initialized in that variable.

Reusability: The encapsulated code is more flexible and easy to change with the new requirements.

  • It Prevents other classes to access the private fields.
  • It also allows us to modify the implemented code without breaking other’s code. Those have already implemented the code.
  • It improved the maintainability of the program and application.
  • If you require to define the fields as read-only, then you can do that by similarly defining only getter methods if you want the fields as only write-only, then you can also do that by only mentioning the setter method.

Ref: article

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers