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
- encapsulation
- inheritance
- polymorphism
See Chapter 14 for detail
Two commonly used permissions are:
- public
- 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:
[static]* [abstract]* ...
P's : public, private, protected, private protected, "package"
-
public - any and all classes can access
(for classes, one usually starts out with this until know why to protect)
- public void AnyOneCanAccess() {}
-
private - accessible only to those within the class they are defined. They are not available to subclasses.
- private String CreditCardNumber;
-
private protected (gone ?) - the IVs and methods are accessible only to that class and subclasses of the class inside and outside package.
- private protected void OnlySubClasses () {}
-
protected - all classes in package and subclasses of the class inside and outside package.
-
"friendly" - no specific declaration. I.e., the default
Also known as "package" since it allows access to any objects inside the same package.
- void MyPackageMethod() {}
| 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):
class Document extends File {
private static int version = 10;
public static void SetVersion (int v) {
version = v; }
}
Elsewhere
Document d1, d2, d3;
d1 = new Document();
d2 = new Document();
d3 = new Document();
Document.SetVersion(5);
// calling by class; can we do this?
d3.SetVersion(5);
// calling by instance; can we do this?
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 Document extends File {
private static int version = 10;
int number_of_chapters;
public static void SetVersion (int v) {
version = v; }
static void add_a_chapter () {
number_of_chapters++; // won't work (CM, IV)
}
static void modify_rev (int i) {
version++; // will work (CM, CV)
}
}
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.