Access modifier

from Wikipedia, the free encyclopedia

Access modifiers (Engl. Access specifier , access modifier or shortly modifier ) are keywords in programming languages that define the degree to access other parts of a program (such as variables, functions or classes) in the parts of a program. They regulate the visibility of these parts within a program.

They are only widely used in object-oriented languages , although some of them also exist in functional programming languages .

Static in functional programming

The only common access modifier in functional programming languages ​​is static(but only in one of four meanings). The access restriction is done via the linker, not the compiler, and is mostly used to prevent a variable from being used across files. In the remaining three meanings it staticdoes not act as an access restriction.

Access modifiers in object-oriented programming languages

In general, there is extensive use of access modifiers in object-oriented programming languages. They also represent the heart of the encapsulation concept. Compliance is monitored by the compiler. However, they do not offer complete protection in the sense of protection against malicious attacks; for example, it is possible in languages ​​that allow pointers to circumvent these restrictions with relatively little effort. Rather, they serve to simplify maintenance and to ensure the traceability and reusability of code (for example, if the functionality has to be expanded or adapted) for the programmer. The restrictions generally depend on how the class of the caller is related to the class of the member to be called.

The following access modifiers are common in object-oriented programming languages:

Access possible for use Typical spellings
All classes Functionalities that the class should provide (class interface) public(C ++, Java, C #), Public(VB.Net)
Heirs only Functionalities that the programmer has to access or that the programmer has to change if he wants to extend the functionality of the class according to the inheritance concept. protected(C ++, Java, C #), Protected(VB.Net), sometimes toofamily
Only within the class Functionalities which, according to the principle of encapsulation, do not have to be known to the outside world. private(C ++, Java, C #), Private(VB.Net)

There are also access modifiers that work outside of the inheritance concept. Their use makes sense in exceptional cases, but is often considered a bad programming style or an indicator of bad program design. These include the following modifiers:

Access possible for use Typical spellings
Classes from the same package / assembly Only special cases: The entire DLL has access to the method. Programs that want to use the DLL, however, have no access. without keyword (Java), internal(C #), Friend(VB.Net), sometimes alsoassembly
A specific class
on all class members
Only special cases: friend [Klasse]makes [Klasse]friends, this means that it has [Klasse]access to all (including private) members of a class ( friend function ) (only in C ++)

staticIn contrast to functional programming in object-oriented programming, it is not an access modifier , but a memory modifier (see: static (keyword) #static and shared for class members ).

Access modifiers can often not be combined, since this is usually not useful. However, where this makes sense, there may be exceptions. For example, in C # and VB.Net, the protectedand the internalmodifier can be combined to allow both inheritors outside the assembly and classes in the same assembly that do not inherit from the same class to access a member.

In most programming languages, access modifiers are specified directly in the declaration of the member. An exception is C ++, where functions with the same access modifiers are grouped in blocks, these blocks then having a common access modifier. These blocks are then introduced accordingly with public:, protected:or private:.

Language-specific features

In Java e.g. For example, a code file can contain any number of classes, but only one of them can be public . Only this one class is visible to the user of the file and can therefore be used. However, multiple publicly visible classes must also use multiple files.

In C ++ , the friend keyword allows other classes to access private members.

Example in C ++

//Header:
class AnotherClass;
class C
{
friend AnotherClass;
public:
    int i;
    float k;
    int m;
protected:
    int s;
private:
    int p;
};
class D : public C
{
    //Klasse kann auf i, k, m und s zugreifen.
    //Klasse kann nicht auf p zugreifen.
};
class E
{
    //Klasse kann auf i, k und m zugreifen.
    //Klasse kann nicht auf s und p zugreifen.
};
class AnotherClass
{
    //Klasse kann auf i, k, m, s und p zugreifen.
};

Example in C #

public class Class1
{
    //Arten der Anwendung auf Methoden:
    public void MethodPu() {/*...*/}
    protected int PropertyPr {get; private set;}
    internal void MethodInt() {/*...*/}
    protected internal void MethodPrInt() {/*...*/}
    private void MethodPri() {/*...*/}

    //Anwendung des Schlüsselwortes auf Klassen:
    private class Subclass : SubclassVorlage
    {
        public int Test {get; set;}
        public overrides int GetNumber() {return 3;}
        void Example()
        {
            //Zugriff auf alle Member von Class1, sowie auf Subclass und Subclass. Test möglich.
        }
    }
    public SubclassVorlage GetNewSubClass() {return new Subclass();}
    //public Subclass GetSubClassDirekt() //Kompilierfehler: Unzulässiger Rückgabewert: Methode ist public, Subclass ist aber private.
    private Subclass GetSubClassDirekt2() {return new Subclass();} //Methode als private allerdings möglich.
    public SubclassVorlage GetNewSubClass2() {return GetSubClassDirekt2();} //Zulässig, nur Rückgabe-/Übergabewerte
    //    Müssen gleiche oder höhere Zugriffsrechte haben, der Code der Methode darf auch private Methoden verwenden.

    //Auswirkungen der Anwendung des Schlüsselwortes auf Methoden in derselben Klasse:
    public void Example()
    {
        //Zugriff auf alle Member von Class1, sowie auf Subclass und Subclass. Test möglich
    }
}
public class SubclassVorlage
{
    public virtual int GetNumber() {return 2;}
}
public class ClassInherit : Class1
{
    //Auswirkungen der Anwendung des Schlüsselwortes auf Methoden in einer erbenden Klasse:
    public void Example()
    {
        this.MethodPu(); //Zugriff möglich
        int v = this.PropertyPr; //Getter möglich
        //this.PropertyPr = v + 1; //Zugriff auf privaten Setter nicht möglich
        this.MethodInt(); //Zugriff auf interne Methode nur möglich, wenn in derselben DLL programmiert.
        this.MethodPrInt(); //Zugriff auf protected-Member innerhalb und außerhalb der DLL möglich
        //this.MethodPri(); //Zugriff auf private Methode nicht möglich.
        //Class1.Subclass c = new Class1.Subclass(); //Zugriff auf private Subklasse nicht möglich, da der
        //   Klasseninhalt nur für Class1 bekannt ist, daher auch Zugriff auf Class1.Test(); nicht möglich.
        SubclassVorlage w = this.GetNewSubClass(); //Sichtbarkeit der Basisklasse von Class1.Subclass gegeben.
        int x = w.GetNumber(); //Resultat: 3: Eingeschränkte Sichtbarkeit hat keinen Einfluss auf Vererbungskonzept.
    }
}
public class ClassNotInherit
{
    //Auswirkungen der Anwendung des Schlüsselwortes auf Methoden in einer unbeteiligten Klasse:
    public void Example()
    {
        this.MethodPu(); //Zugriff möglich
        //int v = this.PropertyPr; //Zugriff nicht möglich, da die Klasse kein Erbe ist.
        this.MethodInt(); //Zugriff auf interne Methode nur möglich, wenn in derselben DLL programmiert.
        this.MethodPrInt(); //Zugriff nur in derselben DLL, da die Klasse kein Erbe ist.
        //Class1.Subclass c = new Class1.Subclass(); //Zugriff auf private Subklasse nicht möglich.
    }
}

See also

Individual evidence

  1. access specifiers , cppreference.com
  2. Access Modifiers (C # Reference)
  3. Controlling Access to Members of a Class (The Java ™ Tutorials> Learning the Java Language> Classes and Objects)
  4. Private, Protected, Public, and Published Declarations - RAD Studio