MorkaLork Development

Interesting stuff I've picked up over the years...

Access modifiers

2009-04-16 18:02:32 | 184 views | access mod modify modifiers public private internal protected instantiated derriving non-derriving constructor hide hidden

What are accessmodifiers?

Access modifiers is a concept that allows you, a programmer, to hide code from other developers that might use your code. It allows you to determine the visibility of your code so that the right person can see it, but so that people who shouldn't use it, or even know it exists, can't see it.

For example, if you're making a class, Person, that is inherited by two other classes, Student and Teacher, then maybe what happens in Person, should stay in Person. Maybe the contents of Person shouldn't be visible to the user of Student and Teacher since a Person is to general of a concept. Then it might be a good idea to hide Person. This can be done by modifying the accessability of the Person-class constructor so that it can't be instantiated from non-derriving classes. Now Person is hidden from the user and only Student and Teacher is available.

There are four main Access Modifiers:

(there is a fifth, protected internal, but if you know what that is, you don't need to read this article)

Public: This can be seen by all and used by all. Total access.
Private: This is visible only inside the current class. A private constructor for example serves no purpos since no class what-so-ever can use it.
Protected: This can be seen only inside the the current class and all the derriving class.
Internal: Visible only within the current assembly.

Table of torment

Member ofDefaultAllowed
enum public None
class private public, private, protected, internal, protected internal
interface public None
Struct private public, internal, private

Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.