Access modifier /Access specifier in java - private, package-private(Default), protected and public - Diagram and tabular form


Access modifiers in java -
  • private,
  • protected and
  • public

Access specifier is synonymous of Access modifier. Please refer Java API doc


Diagram shows Access modifiers in java >
private, protected and public are Keywords in java language.


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>


No comments:

Post a Comment