Showing posts with label modifiers. Show all posts
Showing posts with label modifiers. Show all posts

Tuesday, January 15, 2013

Know About Java Modifiers

As we all know java has below modifiers :

  • public
  • private
  • protected
  • no modifiers (package private)

Class level Modifiers (only class) :

  • If a class is ‘public’, then it CAN be accessed from ANYWHERE.
  • If a class has ‘no modifer’, then it CAN ONLY be accessed from ‘same package’.
Member Level Modifiers (variables & methods):
  • public and no modifier – the same way as used in class level.
  • private – members CAN ONLY access.
  • protected – CAN be accessed from ‘same package’ and a subclass existing in any package can access.
Please see below table :


Access Modifiers

Same Class Same Package Subclass Other packages
public Y Y Y Y
protected Y Y Y N
no access modifier Y Y N N
private Y N N N