Visibility Modifiers

Python Java
All items defined within a class (variables or methods) are potentially visible to programmers who use the class.

Implementers of a class usually discourage direct access to variables by using the underscore in their names.

There are four levels of access to classes and the items defined within them. There are three visibility modifiers that specify access: public, private, and protected.

Public access allows any program component to reference an item.

Example:

public static final int NUM_GRADES = 0;

Private access allows access to an item only by other items within the enclosing class definition.

Example:

private int[] grades;

Protected access extends access to an item from the defining class to all subclasses.

Example:

protected String name;

When a visibility modifier is omitted, the item has package access. This means that access is extended from the defining class to all components in the same package. For most purposes, package access is equivalent to public access.

© Ken Lambert. All rights reserved.