Member Access Modifiers:
- Methods and instance (non local) variables are known as “members.”
- Members can use all four access levels: public, protected, default, and
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 super class.
- 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 super class 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 sub classes, so private members cannot
be inherited. - Default and protected members differ only when sub classes 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 sub classes regardless of package.
- protected = package + kids (kids meaning sub classes).
- For sub classes 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 super class instance.
- 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 sub classes.
Other Modifiers—Members:
- final methods cannot be overridden in a subclass.
- abstract methods are declared with a signature, a return type, and an optional throws clause, but they are not implemented.
- abstract methods end in a semicolon – no curly braces.
- Three ways to spot a nonabstract method:
- The method is not marked abstract.
- The method has curly braces.
- The method MIGHT have code between the curly braces.
- The first nonabstract (concrete) class to extend an abstract class must implement all of the abstract class’ abstract methods.
- The synchronized modifier applies only to methods and code blocks.
- synchronized methods can have any access control and can also be marked final.
- abstract methods must be implemented by a subclass, so they must be inheritable. For that reason:
- abstract methods cannot be private.
- abstract methods cannot be final.
- The native modifier applies only to methods.
- The strictfp modifier applies only to classes and methods.