Access modifiers allow one to restrict the access to a class member or to a class/interface.
There are 4 types of access modifiers :
- package (the default access modifier)
- public
- protected
- private
A 'package' access modifier indicates that the member (or the class/interface) is only accessible by the member of the current package.
A 'public' access modifier indicates that the member is accessible by any other class.
A 'protected' access modifier indicates that only derivates can access this member.
A 'private' access modifier indicates this member is accessible by no one, except the current class.
Example :
package somePackage;
class Toto implements SomeInterface { private int someData; public sub f() {} protected sub g(); }
Class Toto can only be accessed by classes of package 'somePackage'. Field someData can only be manipulated by Toto. Method f() can be called by anyone. Method g() can only be called by Toto and Toto's derivates.
Comments
Java made a mistake here
Thursday, 11. August 2005 20:36:17, by Vinecnt
The way C++ use labels forces programmers to group fields and methods by their access modifiers. It is something that helps keeping a class well organized.
I'm not sure
Friday, 12. August 2005 09:38:20, by David