CSCI15a: permissions

CSCI111: Permissions


Permissions and Scope


The use of defining classes and specifying permissions is how object-oriented languages achieve data-abstraction, encapsulation, information hiding...all of which promote modularity.

The central ideas of OOP are

  1. encapsulation
  2. inheritance
  3. polymorphism
See Chapter 14 for detail

Two commonly used permissions are:

  1. public
  2. private

Variables defined in a method are local to the method and are not known outside of the method. This is called the variables scope. Method variables are implicitly private to the method in which they are defined.

For classes, any instance variables of the class can be "seen" by any methods of that class. However, the private variables (and methods) can only be seen (or accessed) by other methods of that same class...hence private.

Public means accessible by any class

Typical order:

<access> [static]* [abstract]* ...

P's : public, private, protected, private protected, "package"

Modifier Visibility (page 148, Exploring Java; page 73 Nutshell)
public All classes
private None (only within own class)
private protected ? Subclasses only, inside or outside package
protected Classes in package and subclasses inside or outside package
none (default) Classes in same package

What is the difference between a Class Variable and an Instance Variable? Example: salary and topSalary

Another example about who can access what (class vs instance variables and methods):

Elsewhere

if a variable is an instance variable, there are copies in each object
if a variable is a class, there is only one copy

Note:

If you declare a method static, it prevents the method from accessing any variables of the class which are not also declared static (i.e., class methods cannot change instance variables):

Class Types: (Nutshell page 46)

abstract
An abstract class must have at least one abstract method. An abstract class cannot be instantiated, and must be subclassed
final
A final class is declared as the last class of any subclass chain. (efficiency) Classes declared final may not be subclassed. The Math class is a final class
public
Can be accessed by other classes, either directly or subclassing. The class must be imported to be used by other packages, otherwise it is only available within the package where it is declared
synchronized
This class modifier declares that the methods defined in the class are all synchronized.