Encapsulation in OOP

Understanding data hiding and access control in Object-Oriented Programming

What is Encapsulation?

Encapsulation is one of the fundamental principles of Object-Oriented Programming that bundles data and methods that operate on that data into a single unit (class). It restricts direct access to some of the object's components, which is a way of preventing accidental modification of data.

Encapsulation क्या है? (हिंदी में)

Encapsulation OOP का एक महत्वपूर्ण सिद्धांत है जो डेटा और उस पर काम करने वाले methods को एक class में बांधता है। यह डेटा को सुरक्षित रखने का एक तरीका है जिससे गलती से डेटा में बदलाव न हो सके।

Key Points:

  • Data Hiding: Variables are kept private
  • Access Control: Methods are made public
  • Data Protection: Direct access to data is prevented
  • Code Organization: Related data and methods are grouped together

Simple Example

#include <iostream>
#include <string>

class Student {
private:
    std::string name;
    int rollNo;
    float marks;

public:
    // Constructor
    Student(const std::string& n, int r, float m) 
        : name(n), rollNo(r), marks(m) {}
    
    // Getter methods
    std::string getName() const { return name; }
    int getRollNo() const { return rollNo; }
    float getMarks() const { return marks; }
    
    // Setter methods with validation
    void setMarks(float m) {
        if (m >= 0 && m <= 100) {
            marks = m;
        } else {
            std::cout << "Invalid marks! Marks should be between 0 and 100" << std::endl;
        }
    }
};

int main() {
    Student s("Rahul", 101, 85.5);
    
    // Accessing data through getters
    std::cout << "Name: " << s.getName() << std::endl;
    std::cout << "Roll No: " << s.getRollNo() << std::endl;
    std::cout << "Marks: " << s.getMarks() << std::endl;
    
    // Updating marks with validation
    s.setMarks(92.5);  // Valid marks
    s.setMarks(150);   // Invalid marks
    
    return 0;
}

उदाहरण की व्याख्या (Example Explanation)

इस उदाहरण में Student class के माध्यम से encapsulation को समझाया गया है:

  • Private Data: name, rollNo, और marks private variables हैं
  • Public Methods: getName(), getRollNo(), getMarks() getter methods हैं
  • Data Validation: setMarks() method में marks की validation की गई है
  • Controlled Access: डेटा को केवल methods के माध्यम से ही access किया जा सकता है

Benefits of Encapsulation

Encapsulation के फायदे

  1. डेटा सुरक्षा (Data Security)
    • Private variables को बाहर से access नहीं किया जा सकता
    • डेटा में अनचाहे बदलाव को रोका जा सकता है
  2. कोड में लचीलापन (Code Flexibility)
    • Internal implementation को बदला जा सकता है
    • बाहरी code पर कोई प्रभाव नहीं पड़ता
  3. कोड का पुनः उपयोग (Code Reusability)
    • Encapsulated code को दूसरी जगहों पर use किया जा सकता है
    • Code maintenance आसान हो जाता है

Best Practices

  • Always make data members private
  • Provide public getter and setter methods
  • Include validation in setter methods
  • Keep methods focused and single-purpose
  • Use meaningful names for methods