Access modifiers
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.
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. 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
- Access Modifiers - C# Programming Guide · retrieved 2020-01-12 · rev 1331517881
Cite this reference
MLA "Access Modifiers - C# Programming Guide." Wagner, Bill. 2020-01-12. https://en.wikipedia.org/wiki/Bill_Wagner_(software). Accessed 24 November 2026.
Chicago "Access Modifiers - C# Programming Guide." Wagner, Bill. 2020-01-12. https://en.wikipedia.org/wiki/Bill_Wagner_(software).
APA Wagner, Bill. (2020). Access Modifiers - C# Programming Guide. Retrieved November 24, 2026, from https://en.wikipedia.org/wiki/Bill_Wagner_(software)
- Access specifiers · retrieved 2020-01-12 · rev 1331517881
Cite this reference
MLA "Access specifiers." 2020-01-12. https://en.cppreference.com/w/cpp/language/access. Accessed 24 November 2026.
Chicago "Access specifiers." 2020-01-12. https://en.cppreference.com/w/cpp/language/access.
APA encyclopegia.org. (2020). Access specifiers. Retrieved November 24, 2026, from https://en.cppreference.com/w/cpp/language/access
- Access Modifiers (C# Reference) · retrieved 2023-02-07 · rev 1331517881
Cite this reference
MLA "Access Modifiers (C# Reference)." 2023-02-07. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/access-modifiers. Accessed 24 November 2026.
Chicago "Access Modifiers (C# Reference)." 2023-02-07. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/access-modifiers.
APA encyclopegia.org. (2023). Access Modifiers (C# Reference). Retrieved November 24, 2026, from https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/access-modifiers
- Access Control — The Swift Programming Language (Swift 5.3) · retrieved 2021-02-07 · rev 1331517881
Cite this reference
MLA "Access Control — The Swift Programming Language (Swift 5.3)." "Access Control — The Swift Programming Language (Swift 5. 2021-02-07. https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html. Accessed 24 November 2026.
Chicago "Access Control — The Swift Programming Language (Swift 5.3)." "Access Control — The Swift Programming Language (Swift 5. 2021-02-07. https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html.
APA "Access Control — The Swift Programming Language (Swift 5. (2021). Access Control — The Swift Programming Language (Swift 5.3). Retrieved November 24, 2026, from https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html
- Mayo 2002 , pp. 199. Wikipedia revision 1331517881, retrieved 2026-11-19 · retrieved 2026-11-18 · rev 1331517881
Cite this reference
MLA "Mayo 2002 , pp. 199. Wikipedia revision 1331517881, retrieved 2026-11-19." Mayo 2002 , pp. 2026-11-18. https://en.wikipedia.org/w/index.php?oldid=1331517881. Accessed 24 November 2026.
Chicago "Mayo 2002 , pp. 199. Wikipedia revision 1331517881, retrieved 2026-11-19." Mayo 2002 , pp. 2026-11-18. https://en.wikipedia.org/w/index.php?oldid=1331517881.
APA Mayo 2002 , pp. (2026). Mayo 2002 , pp. 199. Wikipedia revision 1331517881, retrieved 2026-11-19. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1331517881
Cite this article
MLA
"Access modifiers." encyclopegia.org, 2026, https://encyclopegia.org/wiki/access-modifiers. Accessed 24 November 2026.
Chicago
"Access modifiers." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/access-modifiers.
APA
encyclopegia.org. (2026). Access modifiers. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/access-modifiers
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON