Access modifiers
Written by:
maffelu
, 2009-04-16 18:02:32
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:
- Public
- Private
- Protected
- Internal
(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 of | Default | Allowed |
| enum |
public |
None |
| class |
private |
public,
private,
protected,
internal,
protected internal |
| interface |
public |
None |
| Struct |
private |
public,
internal,
private |
What are access
2011-04-14 05:35:47
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:
* Public
* Private
* Protected
* Internal
(th