Access modifiers in java -
- private,
- protected and
- public
Diagram shows Access modifiers in java >
Tabular form to show where Access modifiers can be accessed in java >
Access modifier
|
In same class
|
Other classes of same package
|
classes of other packages
|
private
|
Yes
|
No
|
No
|
package-private
(no modifier-
also known as default scope)
|
Yes
|
Yes
|
No
|
protected
|
Yes
|
Yes
|
Yes, but only by inheritance, i.e. in subclasses.
Cannot be accessed by creating object of class.
|
public
|
Yes
|
Yes
|
Yes
|
Access modifiers during method overriding >
when overriding method of subclass Must not have more restrictive access modifier.
Summary >
- private can be accessed only within the same class.
- Default can be accessed only within the classes of same package.
- Protected can be accessed within the classes of same package and in classes of other packages only by inheritance, i.e. in subclasses.
- Public are accessible from everywhere.
/** JavaMadeSoEasy.com */
RELATED LINKS>
Labels:
Core Java