Encapsulation In Java In Hindi

📔 : Java 🔗

data members को single Object में bind करना ही encapsulation कहलाता है। Encapsulation class में defined properties & methods को protect करने की protection mechanism होती है। Encapsulation mechanism की help से हम class members पर अपनी need के according access restrictions define करते हैं , जिससे sensitive data को बाहर से access नहीं किया जा सके।


Encapsulation का simply मतलब होता है , End User से Program की implementation details को hide करना और End User को सिर्फ कुछ जरूरी information को show कराना । Encapsulation के द्वारा ही data को External access से protect किया जाता है। जिसमे class properties को private define करते हैं और public methods के through उन properties को update / modify करते हैं।


Java programming language में हम data members को private define करके fully encapsulated class बनाते हैं। फिर setter और getter methods का use data को set और get करने के लिए करते हैं।

Java Encapsulation Example

File : Employee.java

CopyFullscreenClose FullscreenRun
public class Employee {
  /*private proerties to restrict access*/
  private String name;
  
  /*getter method to get data.*/
  public String getName() {
    return name;
  }

  /*setter method to set data.*/
  public void setName(String newName) {
    this.name = newName;
  }
}

File : Test.java

CopyFullscreenClose FullscreenRun
public class Test {
  public static void main(String[] args) {
    Employee empObj = new Employee();
    // set data using setter method.
    empObj.setName("Rahul Rajput");
    // now get data using getter method.
    String name = empObj.getName();
    System.out.println("Name : "+ name);
  }
}
Output
javac Test.java
java Test
Rahul Rajput

Java Advantages Of Encapsulation

  • Data Hiding And Abstraction

    Encapsulation use करके internal representation & implementation details को End user से hide किया जाता है , जिससे data structure को protect किया जा सके। किसी भी किसी class के members को private बनाकर इसके child class के access से रोका जा सकता है।

  • Reduces Code Complexity

    अब चूंकि class members को access modifiers का use करके एक single unit में bind करते हैं , जिससे code complexity भी reduce होती है।

  • Data Security

    Class properties/Methods को access modifiers का use करके need के according ही members का scope decide कर सकते हैं।

  • Code Reusability

    Code Re-usability भी रहती है , same operation perform करने के लिए आपको code rewrite नहीं करना पड़ता है।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers