Access Modifiers

=====================

Introduction


Access Modifiers are keywords or phrases used in programming languages to control access to Variables, Functions, and other identifiers within a Program. They provide fine-grained control over how code is compiled, linked, and executed, enabling developers to manage different levels of accessibility based on specific requirements.

History


The concept of Access Modifiers dates back to the early days of object-oriented programming (OOP). The first Access Modifier was introduced in C++ by Bjarne Stroustrup in 1985 as a way to control access to class Variables and Functions. Since then, various programming languages have adopted similar concepts, each with its unique set of Access Modifiers.

Access Modifiers


Public Access Modifier

The <a href="/Public" class="missing-article">Public</a> Access Modifier is the most accessible type of Access Modifier, allowing any code to access an identifier without Declaration. When a Variable or function is declared as Public, it can be accessed from anywhere in the Program, regardless of its Scope.

int <a href="/MyPublicVariable" class="missing-article">MyPublicVariable</a> = 10;

Private Access Modifier

The <a href="/Private" class="missing-article">Private</a> Access Modifier restricts access to an identifier within the same module (in C++) or package (in Java). Only code within the same file or package can access a Private Variable or function.

<a href="/Private" class="missing-article">Private</a> int <a href="/MyPrivateVariable" class="missing-article">MyPrivateVariable</a> = 20;

Protected Access Modifier

The <a href="/Protected" class="missing-article">Protected</a> Access Modifier provides a level of accessibility between Public and Private. Code within the same module can access Protected Variables, but not outside the same module.

<a href="/Public" class="missing-article">Public</a> class <a href="/MyClass" class="missing-article">MyClass</a> {
    <a href="/Protected" class="missing-article">Protected</a> int <a href="/MyProtectedVariable" class="missing-article">MyProtectedVariable</a> = 30;
}

Example Use Cases


Access Modifiers in C++

#include <iostream>

class <a href="/MyClass" class="missing-article">MyClass</a> {
<a href="/Public" class="missing-article">Public</a>:
    void printHello() {
        std::cout << "Hello" << std::endl;
    }
};

int <a href="/main" class="missing-article">main</a>() {
    <a href="/MyClass" class="missing-article">MyClass</a> obj;
    obj.printHello();
    return 0;
}

Access Modifiers in Java

<a href="/Public" class="missing-article">Public</a> class <a href="/MyClass" class="missing-article">MyClass</a> {
    <a href="/Private" class="missing-article">Private</a> int <a href="/MyPrivateVariable" class="missing-article">MyPrivateVariable</a> = 40;

    <a href="/Public" class="missing-article">Public</a> void printHello() {
        [System](/System).out.println("Hello");
    }

    <a href="/Public" class="missing-article">Public</a> [Static](/Static) void <a href="/main" class="missing-article">main</a>(String[] args) {
        <a href="/MyClass" class="missing-article">MyClass</a> obj = new <a href="/MyClass" class="missing-article">MyClass</a>();
        obj.printHello();
    }
}

Best Practices


  • Use <a href="/Public" class="missing-article">Public</a> Access Modifiers for Variables and Functions that need to be accessed from outside the module.
  • Use <a href="/Private" class="missing-article">Private</a> Access Modifiers for internal implementation details or data members.
  • Use <a href="/Protected" class="missing-article">Protected</a> Access Modifiers when a class needs to provide Public functionality but not Private access.

Conclusion


Access Modifiers play a crucial role in controlling accessibility within programming languages. By using these keywords and phrases, developers can create modular codebases with fine-grained control over who has access to what. Remember to choose the right Access Modifier for each scenario to ensure clear communication between team members and maintainability of your codebase.

References