Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com

Essence.

To change a component's colour, you have to use its setBackground method, for example :
  but.setBackground ( Color.yellow ) ;

You can't do this :
  but.background = Color.yellow ;

because you have no direct access to the background reference variable.

If you want to know its current value, you'll have to use the getBackground method :
  curcol = but.getBackground ( ) ;

The set- and getBackground methods are specified as being publicly accessible, meaning, that they can be called from any method in any class.

The background variable can ONLY be ACCESSED THROUGH THESE METHODS FROM OTHER CLASSES ! ....

it is protected from direct access,....

it is ENCAPSULATED.

Many methods that you've encountered so far, are public :
paint
actionPerformed
init
set-, get-methods in java.awt.Component
Math.random
etc...

============

The applet class contains three references, specified as private. This means that these can only be accessed from methods in the AccessApplet class itself.

We have another class named Access, extending Panel

and a class Accessextension, that extends Access.

Each one of these contains a reference to a Label, lbl and lbx, with unspecified, default access.

They can be accessed from the AccessApplet class,

because all 3 classes are in the same package, testpak ;

.....................

Default access is
  package

.....................

By clicking in the TextField the Accessextension class accesses the Button reference in its superclass, Access.

It may do so, because it has been derived from this class, it is a subclass ....

and the Button reference has been specified as protected.

To sum it all up :

- public : accessible from any method anywhere

- private : only from methods in the same class

- protected : like private, but also from methods in a derived class, a subclass, an extending class.

- package : like private, but also from methods of classes in the same package.

===========

IMPORTANT :

Instances of the Access and Accessextension classes may only be constructed by classes in the same package because the constructors' access is default : package .

===========

Rule of thumb :

- Data private or protected, access to data preferrably through set-, get-methods.

- Methods that may be called from classes whatsoever : public . These constitute the class' interface to the soft world, to using objects. ( We're not referring to the Java interface concept by this ! )

- Other methods package, protected or even private.

===========

NOTES

===========

Sometimes it's convenient to have public data. A case in point is the java.awt.Point class.

This has two public ints, x and y, so these can be accessed directly :
  ... pt.x ...

Same goes for java.awt.Dimension.

===========

The background variable in java.awt.Component can be accessed from methods in classes in the java.awt package, because its access is default : package.

.