Sobes.tech
Junior — Middle

Can you explain what the access modifier 'private' means in programming?

sobes.tech AI

Answer from AI

The access modifier private in programming (for example, in C++ or Java) restricts access to class members (variables or methods) only within the class itself. This means that such members cannot be accessed directly from code outside the class or from derived classes.

This is used for encapsulation of data to protect the internal state of the object from incorrect external modification.

Example in C++:

class MyClass {
private:
    int secretValue;
public:
    void setValue(int v) { secretValue = v; }
    int getValue() { return secretValue; }
};

MyClass obj;
obj.setValue(10); // correct
// obj.secretValue = 5; // error, access denied