access modifer - ibrahimrifats/Back-End-development GitHub Wiki
Access modifiers are used in programming languages to control the visibility and accessibility of classes, methods, and variables within a program. Different programming languages have their own sets of access modifiers, but here are some common ones found in various languages:
-
Public: The public access modifier allows a class, method, or variable to be accessible from anywhere in the program, including from other classes and external code.
-
Private: The private access modifier restricts the visibility of a class member (method or variable) to only within the class itself. It cannot be accessed from outside the class.
-
Protected: The protected access modifier allows a class member to be accessible within the class and its subclasses (derived classes or subclasses) but not accessible from outside.
-
Internal: This access modifier is specific to languages like C# and allows access to class members within the same assembly (a compiled unit of code) but not from external assemblies.
-
Package-Private (Default in Java): In languages like Java, if no access modifier is specified, the class member has package-private access, which means it is accessible within the same package but not from outside the package.
-
Friend (C++): In C++, the "friend" keyword allows a function or class to have access to private and protected members of a class as if it were a member of that class.
| Input | Output |
|---|---|
| keyword (string) | accessLevel (string) |
| accessType | |
| ------------------- | ----------------------------------------------------------------------- |
| "public" | If accessType is "class", "method", or "variable", |
| Set accessLevel to "anywhere can access". | |
| ------------------- | ----------------------------------------------------------------------- |
| "private" | If accessType is "class", "method", or "variable", |
| Set accessLevel to "restricted to within the class". | |
| ------------------- | ----------------------------------------------------------------------- |
| "protected" | If accessType is "class", "method", or "variable", |
| Set accessLevel to "accessible within the class and its subclasses". | |
| ------------------- | ----------------------------------------------------------------------- |
| "internal" | If accessType is "class", "method", or "variable", |
| Set accessLevel to "accessible within the same assembly (in C#)". | |
| ------------------- | ----------------------------------------------------------------------- |
| Any other value | Set accessLevel to "Invalid access modifier". |
| ------------------- | ----------------------------------------------------------------------- |
Python: Python does not have traditional access modifiers like public, private, or protected. Instead, it uses a single underscore (_) convention to indicate that a variable or method is intended to be used internally within a class, but it doesn't strictly enforce encapsulation.