Jump to content

Access modifiers: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
mNo edit summary
 
(40 intermediate revisions by 29 users not shown)
Line 1: Line 1:
{{Short description|Keywords in object-oriented programming languages}}
{{refimprove|date=March 2013}}
{{refimprove|date=March 2013}}


'''Access modifiers''' (or '''access specifiers''') are [[keyword (computer programming)|keyword]]s in [[object-oriented language]]s 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 (computer programming)|encapsulation]] of components.{{sfn|Mayo|2002|pp=199}}
'''Access modifiers''' (or '''access specifiers''') are [[keyword (computer programming)|keywords]] in [[object-oriented language]]s that set the accessibility of [[Class_(computer_programming)|classes]], [[method_(computer_programming)|methods]], and other members. Access modifiers are a specific part of programming language syntax used to facilitate the [[Encapsulation (computer programming)|encapsulation]] of components.{{sfn|Mayo|2002|pp=199}}


In [[C++]], there are only three access modifiers. [[C Sharp (programming language)|C#]] extends the number of them to five, while [[Java (programming language)|Java]] has four access modifiers,<ref>http://javapapers.com/core-java/access-modifiers-in-java-explain/</ref> but three keywords for this purpose. In Java, having no keyword before defaults to the package-private modifier.
In [[C++]], there are only three access modifiers. [[C Sharp (programming language)|C#]] extends the number of them to six,<ref>{{Cite web|url=https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers|title=Access Modifiers - C# Programming Guide|last=Wagner|first=Bill|authorlink=Bill Wagner (software)|date=|website=docs.microsoft.com|language=en-us|archive-url=|archive-date=|access-date=2020-01-13}}</ref> while [[Java (programming language)|Java]] has four access modifiers, but three keywords for this purpose. In Java, having no keyword before defaults to the package-private modifier.


When a 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. A class 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.
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.

[[File:UML class diagram with access modifiers.svg|UML class diagram with access modifiers.svg]]


== Names of keywords ==
== Names of keywords ==
C++ uses the three modifiers called ''<code>public</code>'', ''<code>protected</code>'', and ''<code>private</code>''. [[C Sharp (programming language)|C#]] has the modifiers ''<code>public</code>'', ''<code>protected</code> '',''<code>internal</code>'', ''<code>private</code>'', and ''<code>protected internal</code>''. [[Java (programming language)|Java]] has ''<code>public</code>'', ''<code>package</code>'', ''<code>protected</code>'', and ''<code>private</code>''. The access modifier ''<code>package</code>'' is the default and used, if any other access modifier keyword is missing. 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.
C++ uses the three modifiers called ''<code>public</code>'', ''<code>protected</code>'', and ''<code>private</code>''.<ref>{{Cite web|url=https://en.cppreference.com/enwiki/w/cpp/language/access|title=Access specifiers|last=|first=|date=|website=en.cppreference.com|archive-url=|archive-date=|access-date=2020-01-13}}</ref> [[C Sharp (programming language)|C#]] has the modifiers ''<code>public</code>'', ''<code>protected</code> '',''<code>internal</code>'', ''<code>private</code>'', ''<code>protected internal</code>'', ''<code>private protected</code>'', and ''<code>file</code>''.<ref>{{cite web |title=Access Modifiers (C# Reference) |url=https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/access-modifiers |website=learn.microsoft.com |publisher=[[Microsoft]] |access-date=2023-02-08 |date=2022-09-27}}</ref> [[Java (programming language)|Java]] has ''<code>public</code>'', ''<code>package</code>'', ''<code>protected</code>'', and ''<code>private</code>''; ''<code>package</code>'' 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.<ref>{{Cite web|title=Access Control — The Swift Programming Language (Swift 5.3)|url=https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html|access-date=2021-02-08|website=docs.swift.org}}</ref>


{| class="wikitable"
{| class="wikitable"
|-
|-
! Keyword !! C# !! C++ !! Java
! Keyword !! C# !! C++ !! Java !! Swift
|-
|-
| '''<code>private</code>''' || class || class || class
| '''<code>private</code>''' || class || class<br/>''and/or''<br/>[[friend class]]es || class || enclosing declaration only<br/>+ extensions of declaration in same file
|-
|-
| '''<code>protected internal</code>''' || same assembly and<br/>derived classes || - || -
| '''<code>fileprivate</code>''' || - || - || - || same file
|-
|-
| '''<code>protected</code>''' || derived classes || derived classes || derived classes<br/>''and/or''<br/>within same package
| '''<code>file</code>''' || same file || - || - || -
|-
|-
| '''<code>package</code>''' || - || - || within its package
| '''<code>private protected</code>''' || derived classes in the same assembly || - || - || -
|-
|-
| '''<code>internal</code>''' || same assembly || - || -
| '''<code>protected internal</code>''' || same assembly<br/>''and/or''<br/>derived classes || - || - || -
|-
|-
| '''<code>protected</code>''' || derived classes || derived classes<br/>''and/or''<br/>friend classes || derived classes<br/>''and/or''<br/>within same package || -
| '''<code>public</code>''' || everybody || everybody || everybody
|-
| '''<code>package</code>''' || - || - || within its package || -
|-
| '''<code>internal</code>''' || same assembly || - || - || same module
|-
| '''<code>public</code>''' || everybody || everybody || everybody || everybody
|-
| '''<code>open</code>''' || - || - || - || everybody <br/>+ subclass outside module<br/>+ override outside module
|}
|}


== Example in C++ ==
== Example in C++ ==
<syntaxhighlight lang="cpp">
#include <iostream>
#include <iostream>
#include<conio.h>
#include <conio.h>
using std::cout;
using std::cout;
using std::endl;
using std::endl;
Line 47: Line 61:
// int get_x() { return x; } // ERROR, B::x is inaccessible here
// int get_x() { return x; } // ERROR, B::x is inaccessible here
private:
private:
using B::f; // D::f is private
};
int main() {
int main() {
D d;
D d;
Line 66: Line 83:
// b.m = 3; // ERROR, B::m is protected
// b.m = 3; // ERROR, B::m is protected


b.set_n(3); // calls B::set_n(int)
</source>
// cout << b.get_n(); // ERROR, 'struct B' has no member named 'get_n'

b.f(); // calls B::f()
return 0;
}

</syntaxhighlight>


== References ==
== References ==

Latest revision as of 13:50, 27 January 2024

Access modifiers (or access specifiers) are 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.[1]

In C++, there are only three access modifiers. C# extends the number of them to six,[2] 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.

UML class diagram with access modifiers.svg

Names of keywords

[edit]

C++ uses the three modifiers called public, protected, and private.[3] C# has the modifiers public, protected ,internal, private, protected internal, private protected, and file.[4] 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.[5]

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 in C++

[edit]
#include <iostream>
#include <conio.h>
using std::cout;
using std::endl;

struct B { // default access modifier inside struct is public
    void set_n(int v) { n = v; }
    void f()          { cout << "B::f" << endl; }
  protected:
    int m, n; // B::m, B::n are protected
  private:
    int x;
};
 
struct D : B {
    using B::m;               // D::m is public
    int get_n() { return n; } // B::n is accessible here, but not outside
//  int get_x() { return x; } // ERROR, B::x is inaccessible here
 private:
    using B::f;               // D::f is private
};
 
int main() {
    D d;

//  d.x = 2; // ERROR, private
//  d.n = 2; // ERROR, protected
    d.m = 2; // protected B::m is accessible as D::m

    d.set_n(2); // calls B::set_n(int)
    cout << d.get_n() << endl; // output: 2

//  d.f();   // ERROR, B::f is inaccessible as D::f


    B& b = d; // b references d and "views" it as being type B

//  b.x = 3; // ERROR, private
//  b.n = 3; // ERROR, protected
//  b.m = 3; // ERROR, B::m is protected

    b.set_n(3); // calls B::set_n(int)
//  cout << b.get_n(); // ERROR, 'struct B' has no member named 'get_n'

    b.f();   // calls B::f()
    return 0;
}

References

[edit]

Notes

[edit]
  1. ^ Mayo 2002, pp. 199.
  2. ^ Wagner, Bill. "Access Modifiers - C# Programming Guide". docs.microsoft.com. Retrieved 2020-01-13.
  3. ^ "Access specifiers". en.cppreference.com. Retrieved 2020-01-13.
  4. ^ "Access Modifiers (C# Reference)". learn.microsoft.com. Microsoft. 2022-09-27. Retrieved 2023-02-08.
  5. ^ "Access Control — The Swift Programming Language (Swift 5.3)". docs.swift.org. Retrieved 2021-02-08.

Bibliography

[edit]