‘Stupid’ Question 24: Can you give me an easy to remember example of what the different access modifiers do?
Access modifiers - it is easy to mix them up now and then. And if you havent been using them, then you should really consider to do so! Why are they so important? Because of the encapsulation thing. Say what???
Oki, in object oriented programming there is something that we refer to as the three pillars (pillar: something strong that provides defining support), those three pillars are polymorphism, inheritance and encapsulation. The first two I’ll discuss in another post. Encapsulation*" means that a group of related properties, methods, and other members are treated as a single unit or object."*
In other words, to make the code safer we hide implementation details. Kind of you would close the door to the bedroom when you have visitors over, so they wont see that the only reason it is tidy in the livingroom is because all the mess got thrown in the bedroom. Why do they need to see the bedroom anyway? So you close the door, but not for everybody. The person or persons that will have to clean up the mess, or use the room should be allowed to do so.
And how can we remember them: Let me introduce you to the party concept:
PUBLIC
The type or member can be accessed by any other code in the same assembly or another assembly that references it.
PRIVATE
The type or member can be accessed only by code in the same class or struct.
PROTECTED
The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.
INTERNAL
The type or member can be accessed by any code in the same assembly, but not from another assembly.
PROTECTED INTERNAL EDIT : why no image? Because the question was about the different access modifiers, and there are four of them. With them you can set five accessability levels, the fifth level being protected internal.
Protected internal:
The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.
Do you have a creative way to explain/remember the basic access modifiers? This was the best I could come up with when explaining to a friend that is just getting started with programming. I made up a story about four different parties or events on facebook and what kind of invitations were sent out :D
Comments
Reflection - You were able to sneak into the party without being noticed!!!!
The saying I was told (*) (for C++) was "A friend can touch your private parts". (*) I actually wasn't told it. I read it in a book. Unfortunately, I can't recall the title or author of the book (but I know exactly which book it is, so I can look it up). The speaker is cited only as "Anonymous (but you'd recognize the name)". Note in C++ "friend" and "private" are keywords.
Ok, I looked it up. "C++ Strategies and Tactics" Robert B. Murray (1993, Addison-Wesley), pg.74 : "Someone who wishes to remain nameless (2) has said: A friend is someone who can touch your private parts. (2) But you'd recognize the name.
Well, usually when I forget that the implicit access modifier for a class is private, and this class is a Model in MVC, the compiler reminds me that this needs to be public :)) Alright, just joking. My rule of thumb is that everything is private or implicit, except the constructor, if not doing some Singleton. As I'm working on the solution I change the access modifier if needed. The easiest way to explain access modifiers is to use them, and using my rule of thumb you will hit walls and see why this needs to be private, protected, public, internal or internal protected.
Last modified on 2012-08-15