jay's old blog

this blog will be deleted soon - please visit my new blog - https://thesanguinetechtrainer.com

Access Modifiers

Without access modifiers, most of the magic of encapsulation would work. A class without properly used access modifiers (or an assembly or a method) is like a house without locks. Anybody can do anything and before you know thing, things are going down the drain.

So, there are a total of five access modifiers. Here is a quick list and what they do.

Public

This means, anybody can use it. It cannot any more simpler than this. Does this mean, another class can just access it, perhaps create issues again. For instance, methods are usually public. So, that would create some problems, you would think. The thing is, methods can be called, but the internals of a method are still under your control.

That means, just because someone can access some members of your type, does not mean bad things will happen. It just means that you give them access to the extent they require.

Internal

This means, the given member is only available in the current assembly.

Okay, so assemblies. Yeah, if you are only beginning to learn programming (which is probably the case if you are reading my blog in its dictated sequence), you don’t need to know about that. However, just remember that when you prefix it, you can only use these within the assembly.

Protected

This keyword helps you restrict the usage to the 'containing class' and 'derived classes'. That means, only the members of said class can use it and those that are derived from said class.

Protected Internal

A combination of the last two. This means, either of the above two. Within assembly or derived.

Private

Usage restricted to containing class.

I don’t have a sample code for this because the stuff is self explanatory.

Follow me on twitter, facebook and instagram for more updates. Thanks!

Comments are closed