Thursday, October 23, 2008

Member Access Modifiers (Objectives 1.3 and 1.4)

Methods and instance (nonlocal) variables are known as "members."
Members can use all four access levels: public, protected, default, private.
Member access comes in two forms:
Code in one class can access a member of another class.
A subclass can inherit a member of its superclass.
If a class cannot be accessed, its members cannot be accessed.
Determine class visibility before determining member visibility.
public members can be accessed by all other classes, even in other packages.
If a superclass member is public, the subclass inherits it—regardless of package.
Members accessed without the dot operator (.) must belong to the same class.
this. always refers to the currently executing object.
this.aMethod() is the same as just invoking aMethod().
private members can be accessed only by code in the same class.
private members are not visible to subclasses, so private members cannot be inherited.

Default and protected members differ only when subclasses are involved:
Default members can be accessed only by classes in the same package.
protected members can be accessed by other classes in the same package, plus subclasses regardless of package.
protected = package plus kids (kids meaning subclasses).
For subclasses outside the package, the protected member can be accessed only through inheritance; a subclass outside the package cannot access a protected member by using a reference to a superclass instance (in other words, inheritance is the only mechanism for a subclass outside the package to access a protected member of its superclass).
A protected member inherited by a subclass from another package is not accessible to any other class in the subclass package, except for the subclass' own subclasses.

No comments: