From Surf Wiki (app.surf) — the open knowledge base
Access modifiers
Keywords in object-oriented programming languages
Keywords in object-oriented programming languages
Access modifiers (or access specifiers) are reserved keywords in object-oriented languages that set the accessibility of classes, methods, and other members. Access modifiers are a specific part of programming language syntax used to facilitate the encapsulation of components.
In C++, there are only three access modifiers. C# extends the number of them to six, while Java has four access modifiers, but three keywords for this purpose. In Java, having no keyword before defaults to the package-private modifier.
When the class is declared as public, it is accessible to other classes defined in the same package as well as those defined in other packages. This is the most commonly used specifier for classes. However, a class itself cannot be declared as private. If no access specifier is stated, the default access restrictions will be applied. The class will be accessible to other classes in the same package but will be inaccessible to classes outside the package. When we say that a class is inaccessible, it simply means that we cannot create an object of that class or declare a variable of that class type. The protected access specifier too cannot be applied to a class.
Names of keywords
C++ uses the three modifiers called public, protected, and private. C# has the modifiers public, protected ,internal, private, protected internal, private protected, and file. Java has public, package, protected, and private; package is the default, used if no other access modifier keyword is specified. The meaning of these modifiers may differ from one language to another. A comparison of the keywords, ordered from the most restrictive to the most open, and their meaning in these three languages follows. Their visibility ranges from the same class to the package where the class is defined to a general access permission. Below, the maximal access is written into the table.
In Swift, there are five different access levels relative to both the source file in which the entity is defined and the module containing that source file.
| Keyword | C# | C++ | Java | Swift |
|---|---|---|---|---|
| `private` | class | class | ||
| and/or | ||||
| friend classes | class | enclosing declaration only | ||
| + extensions of declaration in same file | ||||
| `fileprivate` | - | - | - | same file |
| `file` | same file | - | - | - |
| `private protected` | derived classes in the same assembly | - | - | - |
| `protected internal` | same assembly | |||
| and/or | ||||
| derived classes | - | - | - | |
| `protected` | derived classes | derived classes | ||
| and/or | ||||
| friend classes | derived classes | |||
| and/or | ||||
| within same package | - | |||
| `package` | - | - | within its package | - |
| `internal` | same assembly | - | - | same module |
| `public` | everybody | everybody | everybody | everybody |
| `open` | - | - | - | everybody |
| + subclass outside module | ||||
| + override outside module |
Example
C++
import std;
// default access modifier inside struct is public
// default access modifier inside class is private
struct Base {
private:
int x;
protected:
// Base::m, Base::n are protected
int m;
int n;
public:
void set_n(int v) noexcept {
n = v;
}
void f() {
std::println("calling Base::f()");
}
};
// Derived extends Base with public inheritance
struct Derived : public Base {
private:
using Base::f; // Derived::f is private
public:
using Base::m; // Derived::m is public
// Base::n is accessible here, but not outside
[[nodiscard]]
int get_n() const noexcept {
return n;
}
// ERROR, Base::x is inaccessible here
// int get_x() const noexcept {
// return x;
// }
};
int main(int argc, char* argv[]) {
Derived d;
// d.x = 2; // ERROR, private
// d.n = 2; // ERROR, protected
d.m = 2; // protected Base::m is accessible as Derived::m
d.set_n(2); // calls Base::set_n(int)
std::println("{}", d.get_n()); // output: 2
// d.f(); // ERROR, Base::f is inaccessible as Derived::f
Base& b = d; // b references d and "views" it as being type Base
// b.x = 3; // ERROR, private
// b.n = 3; // ERROR, protected
// b.m = 3; // ERROR, Base::m is protected
b.set_n(3); // calls Base::set_n(int)
// std::println("{}", b.get_n()); // ERROR, 'struct Base' has no member named 'get_n'
b.f(); // calls Base::f()
return 0;
}
References
Notes
Bibliography
References
- Wagner, Bill. "Access Modifiers - C# Programming Guide".
- "Access specifiers".
- (2022-09-27). "Access Modifiers (C# Reference)". [[Microsoft]].
- "Access Control — The Swift Programming Language (Swift 5.3)".
This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page.
Ask Mako anything about Access modifiers — get instant answers, deeper analysis, and related topics.
Research with MakoFree with your Surf account
Create a free account to save articles, ask Mako questions, and organize your research.
Sign up freeThis content may have been generated or modified by AI. CloudSurf Software LLC is not responsible for the accuracy, completeness, or reliability of AI-generated content. Always verify important information from primary sources.
Report