access modifiers
You are here :

Sub nodes

Recent changes
Site plan
 
 

Access modifiers allow one to restrict the access to a class member or to a class/interface.

There are 4 types of access modifiers :

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.

Propulsed by Wiclear

Comments

Java made a mistake here

Thursday, 11. August 2005 20:36:17, by Vinecnt

I think Java made a mistake by placing access modifiers with the declaration.

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

Well sure, it helps organize the class, but it is a mess when it comes to creating a simple parser. In fact it has nothing to do with language features here. My bet is it helps bringing a simpler grammar. That's all.