Java’s access control mechanisms allow you to control dependences.
In the text of a class, you can indicate which other classes can have dependences on it, and to some extent you can control the nature of the dependences.
A class declared as public can be referred to by any other class; otherwise, it can be referred to only by classes in the same package. So by dropping th s modifier, we can prevent dependences on the class from any class outside the package.
Members of a class (fields and methods) may be:
Public: can be accessed from anywhere.
Private: can be accessed only from within the class in which the field or method is declared.
Protected: can be accessed within the package, or from outside the package by a subclass of the class in which the members declared.
Package: can be accessed within the package (it is the default).
Modifiers on members of B allow us to control the nature of dependence by changing which members belong to B‘s specification.
Copyright © 1998-2009 Dilvan Moreira