Access Modifiers
======================
Introduction
Access Modifiers are keywords used to control access to Variables, Functions, Classes, and other elements of a program’s Source Code. They determine what parts of the code can be accessed by other parts of the code, and which ones cannot. There are five basic Access Modifiers in Programming Languages: public, private, protected, internal, and default.
Public Access Modifier
- Public: Allows access to all parts of the class, function, or variable.
- Example: “`cpp class MyClass { public: void publicMethod() { cout << “Hello World!” << endl; } };
### [Inheritance](/Inheritance)
When a subclass inherits from a parent class using [Inheritance](/Inheritance) (e.g., `SubClass::parentClass()`), all [Access Modifiers](/Access_Modifiers) of the parent class are inherited by the subclass.
* ```cpp
class Parent {
public:
void publicMethod() { cout << "Hello World!" << endl; }
};
class SubClass : public Parent {
public:
void privateMethod() { cout << "This is a <a href="/Private_Method" class="missing-article">Private Method</a>." << endl; } // only accessible within SubClass
};
Private Access Modifier
- Private: Prevents access to all parts of the class, function, or variable.
- Example: “`cpp class MyClass { private: void privateMethod() { cout << “This is a Private Method.” << endl; } };
void privateFunction() { // Error: cannot call privateMethod() MyClass obj; obj.privateMethod(); }
### Access from within <a href="/Subclasses" class="missing-article">Subclasses</a>
Private [Access Modifiers](/Access_Modifiers) can only be accessed within the same class.
## Protected [Access Modifier](/Access_Modifier)
-------------------------
* **Protected**: Allows access to all parts of the class, function, or variable.
* Example:
```cpp
class MyClass {
protected:
void protectedMethod() { cout << "This is a <a href="/Protected_Method" class="missing-article">Protected Method</a>." << endl; }
};
void protectedFunction() { // Error: cannot call protectedMethod()
MyClass obj;
obj.protectedMethod();
}
Access from within Subclasses
Protected Access Modifiers can be accessed by Subclasses.
Internal Access Modifier
- Internal: Prevents access to all parts of the class, function, or variable.
- Example: “`cpp class MyClass { internal: void internalMethod() { cout << “This is an Internal Method.” << endl; } };
void internalFunction() { // Error: cannot call internalMethod() MyClass obj; obj.internalMethod(); }
### Access from within <a href="/Subclasses" class="missing-article">Subclasses</a>
Internal [Access Modifiers](/Access_Modifiers) can be accessed by <a href="/Subclasses" class="missing-article">Subclasses</a>.
## Default [Access Modifier](/Access_Modifier)
----------------------
* **Default**: Allows access to all parts of the class, function, or variable.
* Example:
```cpp
class MyClass {
public:
void defaultMethod() { cout << "This is a <a href="/Default_Method" class="missing-article">Default Method</a>." << endl; }
};
void defaultFunction() { // Error: cannot call defaultMethod()
MyClass obj;
obj.defaultMethod();
}
Access from within Subclasses
Default Access Modifiers can be accessed by Subclasses.
Conclusion
In conclusion, Access Modifiers are used to control access to elements of a program’s Source Code and determine which parts can be accessed by other parts. Understanding the five basic Access Modifiers in Programming Languages is essential for writing efficient and secure code.
Example Use Cases
- Accessing Variables from multiple Classes: Inheritance
- Accessing private methods within sub Classes: Private Access Modifier
- Accessing protected methods from within sub Classes: Protected Access Modifier
- Accessing internal methods from within sub Classes: Internal Access Modifier
- Accessing default methods from within sub Classes: Default Access Modifier
Best Practices
- Use Access Modifiers judiciously to control the level of access to sensitive data.
- Consider using Access Modifiers for private and protected members when necessary, but only if they are truly intended to be accessed by Subclasses.
- Avoid using internal or default Access Modifiers unless absolutely necessary.