1

What is 'Access specifier' in Object oriented programming ?

I have searched for it's definition several times but not get the satisfactory answer.

Can anyone please explain it to me with realistic example ?....

Thanks in advance

Arafat
  • 143
  • 1
  • 4
  • 14

1 Answers1

2

What are they?

This wikipedia article pretty much sums it up. But Let's elaborate on a few main points. It starts out saying:

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

So an Access Specifier aka Access Modifier takes certain class, method, or variable and decides what other classes are allowed to use them. The most common Access Specifiers are Public, Protected, and Private. What these mean can vary depending on what language you are in, but I'm going to use C++ as an example since that's what the article uses.

Accessor Method |   Who Can Call It
-----------------------------------------------------------------------
Private         | Only the class who created it
Protected       | The class who created it and derived classes that "inherit" from this class
Public          | Everyone can call it

Why is this important?

A big part of OOP programming is Encapsulation. Access Specifiers allow Encapsulation. Encapsulation lets you choose and pick what classes get access to which parts of the program and a tool to help you modularize your program and separate out the functionality. Encapsulation can make debugging a lot easier. If a variable is returning an unexpected value and you know the variable is private, then you know that only the class that created it is affecting the values, so the issue is internal. Also, it stops other programmers from accidentally changing a variable that can unintentionally disrupt the whole class.

Simple Example

Looking at the example code from the article we see Struct B i added the public in there for clarity:

struct B { // default access modifier inside struct is public
  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;
};

This is what would happen if you created an inherited struct C that would try and use members from struct B

//struct C is going to inherit from struct B
struct C :: B { 
      public:
        void set_m(int v) {m = v}  // m is protected, but since C inherits from B
                                   // it is allowed to access m.
        void set_x(int v) (x = v)  // Error X is a private member of B and 
                                   // therefore C can't change it.
    };

This is what would happen if my main program where to try and access these members.

    int main(){
  //Create Struct
  B structB;
  C structC;

  structB.set_n(0); // Good Since set_n is public
  structB.f(); // Good Since f() is public

  structB.m = 0; // Error because m is a protected member of Struct B 
                 // and the main program does not "inherit" from struct B"

  structB.x = 0; // Error because x is a private member of Struct B

  structC.set_n() // Inheritied public function from C, Still Good
  structC.set_m() // Still Good

  structC.m = 0   // Error Main class can't access m because it's protected.
  structC.x = 0;  // Error still private.

  return 0;
}

I could add another example using inheritance. Let me know if you need additional explanation.

shockawave123
  • 699
  • 5
  • 15