Modifiers
8. Modifiers
8.1. Access modifiers
There are three access modifiers keywords available in Java: * public * protected * private
There are four access levels: public,protected, default and private.
They define how the corresponding element is visible to other components.
If something is declared public, e.g., classes or methods can be freely created or called by other Java objects. If something is declared private, e.g., a method, it can only be accessed within the class in which it is declared.
The access levels protected and defaultare similar. A protected class can be accessed from the package and sub-classes outside the package, while adefault class can get accessed only via the same package.
The following table describes the visibility:
Modifier | Class | Package | Subclass | World |
---|---|---|---|---|
public | Y | Y | Y | Y |
protected | Y | Y | Y | N |
no modifier | Y | Y | N | N |
private | Y | N | N | N |
8.2. Other modifiers
final methods: cannot be overwritten in a subclass abstract method: no method body
synchronized method: thread safe, can be final and have any access control
native methods: platform dependent code, apply only to methods
strictfp: class or method
Comments
Post a Comment