We've been seeing access modifiers throughout the book. You use access modifiers when you declare a class, and when you declare the members of the class. Here they are (note that some of them, like Protected, are designed to be used only with inheritance):
Public- Entities declared Public have public access. There are no restrictions on the accessibility of public entities. You can use Public only at module, namespace, or file level.
Protected- Entities declared Protected have protected access. They are accessible only from within their own class or from a derived class. Protected access is not a superset of friend access. You can use Protected only at class level.
Friend- Entities declared Friend have friend access. They are accessible from within the program that contains their declaration and from anywhere else in the same assembly. You can use Friend only at module, namespace, or file level.
Protected Friend- Entities declared Protected Friend have both protected and friend access. They can be used by code in the same assembly, as well as by code in derived classes. The rules for Protected and Friend apply to Protected Friend as well.
Private- Entities declared Private have private access. They are accessible only from within their declaration context, including from any nested procedures. You can use Private only at module, namespace, or file level.
Public base class members are available to derived classes and everywhere else, private members are available only in the current class-not in classes derived from that class-protected members are available only in the current class and classes derived from that class, and friend members are available throughout the current assembly. For example, note that I declared the MainForm data member in the Animal class as Public:
However, this gives MainForm more scope than it needs, and it's contrary to the idea of encapsulation and data hiding in OOP, which says that objects should be as self-contained as possible (to avoid unintentional naming conflicts or illegal data access). There's no reason all parts of our code should have access to this variable, but classes derived from this class will need access to MainForm, so I make MainForm protected, which restricts its scope to the current class (Animal) and any classes derived from it (like Dog). This is how MainForm is actually declared in the Inheritance example on the CD-ROM:
Public Class Animal Protected MainForm As Form1 Public Sub New(ByVal form1 As Form1) MainForm = form1 End Sub ⋮ End Class