Encapsulation in OOP

Encapsulation

Encapsulation OOP (Object-Oriented Programming) का core concept है, जिसमें Data (Variables) और उस data पर काम करने वाले Methods (Functions) को एक single unit (Class) में bundle किया जाता है, और data को unauthorized access से सुरक्षित रखने के लिए direct access को restrict किया जाता है।

Example : IRCTC Booking System में User अपनी personal details भरता है। Details internally database में store होती हैं, लेकिन user को direct database access नहीं मिलता। Encapsulation ensures कि data केवल authorized methods से access हो।

key Point

  • Bundling of Data and Methods : Encapsulation में Data (Variables) और उन पर काम करने वाले Methods (Functions) को एक single unit (Class) में bundle किया जाता है। इसका उद्देश्य code को modular और manageable बनाना है।

  • Data Hiding : Encapsulation डेटा को external access से hide करता है। Data को private declare किया जाता है, जिससे इसे सीधे class के बाहर access नहीं किया जा सकता। केवल उसी class के methods के माध्यम से ही data तक पहुँच संभव होती है।

  • Controlled Access : outside classes या objects सीधे data को access नहीं कर सकते। उन्हें public methods (जैसे getters और setters) का उपयोग करके ही data से interact करना होता है। यह approach security और integrity सुनिश्चित करती है।

Syntax

class ClassName {
private:
           // Data Members (variables)
          datatype variable1;
          datatype variable2;

public:
   // Setter Methods
   void setVariable1(datatype value) {
        // validation + assignment
         variable1 = value;
}

// Getter Methods
datatype getVariable1() {
         return variable1;
}

// Other Methods
void display() {
   // code
}
};

Encapsulation Example in C++

#include <iostream>
using namespace std;

class Student {
private:
      string name; // Encapsulated (hidden) data
      int age;

public:
      // Setter for name
      void setName(string n) {
            name = n;
}

// Setter for age (with validation)
void setAge(int a) {
         if(a > 0 && a < 120)
            age = a;
         else
            cout << “Invalid Age!” << endl;
}

// Getter for name
string getName() {
return name;
}

// Getter for age
int getAge() {
       return age;
}
};

int main() {
          Student s1;

          s1.setName(“Rahul Kumar”);
          s1.setAge(20);

          cout << “Student Name: ” << s1.getName() << endl;
          cout << “Student Age : ” << s1.getAge() << endl;

          return 0;
}

Explanation

  • name और age को private रखा गया है, इसलिए इन्हें class के बाहर से सीधे access नहीं किया जा सकता।
  • setName() और setAge() methods के जरिए ही data update किया जाता है, जिससे access पूरी तरह controlled रहता है।
  • setAge() में validation दिया है, इसलिए गलत या unrealistic age class में store नहीं होगी।
  • User को सिर्फ public methods का access मिलता है—data को safely hide करना ही Encapsulation कहलाता है।

Key Components (Encapsulation के Components )

1. Data Members (Variables)

  • ये class के अंदर declare होते हैं।
  • Class का internal data इन्हीं variables में stored रहता है।
  • Access modifiers (private, protected, public) के द्वारा इनका access control किया जाता है।

2. Methods (Functions)

  • Methods define की जाती हैं जिससे class के private data को सुरक्षित तरीके से access या modify किया जा सके।
  • ये usually public होती हैं, ताकि outside world authorized तरीके से data access कर सके।
  • Methods में validation logic भी add किया जा सकता है।

3 Access Modifiers (Access Control)

  • Access Modifiers Encapsulation का core हिस्सा हैं। ये define करते हैं कि class के members (variables और methods) कहाँ से access हो सकते हैं।
  • मुख्य रूप से तीन types होते हैं: private, protected, और public।

4. Getter And Setter Methods

  • Getter और Setter methods Encapsulation का सबसे important हिस्सा हैं।
  • ये private data members को safe तरीके से access और modify करने के लिए use होते हैं।

Importance (Encapsulation क्यों Important है?)

1. Data Hiding 

  • Encapsulation के जरिए class के internal data को unauthorized access से बचाया जाता है।
  • Private variables को केवल public methods के जरिए access किया जा सकता है।

2. Maintainability

  • Agar future में किसी variable या method को बदलना हो → सिर्फ class के अंदर change करना होगा।
  • External code जो class use करता है, उससे impact नहीं पड़ेगा।
  • यह software को easy to maintain बनाता है।

3. Flexibility

  • Getter और Setter methods में validation logic add कर सकते हैं।
4. Reusability 
  • Encapsulated classes को other programs में reuse किया जा सकता है।
  • Example : Flipkart, Paytm जैसे apps में modules encapsulated होते हैं।

5. Security and Integrity 

  • Encapsulation ensures करता है कि data corrupt न हो और only authorized methods data modify कर सकें।
  • Real-life analogy : जैसे आप अपनी personal diary या wallet balance सिर्फ authorized way से ही access करते हैं।

Advantages of Encapsulation 

1. Data Security

  • Variables private रहने से unauthorized access नहीं होता।
  • Sensitive data safe रहता है।

2. Controlled Access

  • Getter और Setter से data पर पूरा control मिलता है।
  • Who-can-modify और what-value-allowed दोनों control किए जा सकते हैं।

3. Data Integrity

  • Setter methods में validation logic add कर सकते हैं।
  • गलत/invalid data class में enter नहीं होता।

4. Easy Maintenance

  • Class के अंदर change करने से external code प्रभावित नहीं होता।
  • बड़े projects में maintain करना आसान होता है।

5. Flexibility

  • Future requirements के हिसाब से class के अंदर logic easily बदल सकते हैं।
  • Validation, conditions, formatting add करना आसान।

6. Code Reusability

  • Encapsulated class एक independent module की तरह काम करती है।
  • Different programs/projects में reuse किया जा सकता है

Disadvantages of Encapsulation

1. More Code Overhead

  • हर variable के लिए getter/setter लिखना पड़ता है।
  • Extra lines of code बढ़ जाती हैं।

2. Slight Performance Drop

  • Direct variable access से methods access थोड़ा slow होता है।

3. Good Design Required

  • Encapsulation ठीक से apply करने के लिए planning चाहिए।
  • Beginners को class design में confusion हो सकता है।
error: Content is protected !!