Core Java Tutorial

Core Java Tutorial

What is Access Specifiers in Java

Access modifiers:

Access level modifiers determine whether other classes can use a particular field or invoke a particular method. Access modifiers (or access specifiers) are keywords in object-oriented languages that set the accessibility of classes, methods, and other members. Access modifiers are a specific part of programming language syntax used to facilitate the encapsulation of components

There are four types of access modifiers

  1. public
  2. protected
  3. default or package or no modifiers
  4. private

Public

                Can be accessed anywhere from an application

Protected

                Can be accessed within the class as well as inherited class from different package.

No modifiers

                Can be accessed only within the same package.

Private

                Can be access only within the same class.

Refer below diagram for details.

In class you can use public or no modifiers. whereas member variable, methods, constructor can use all the four access modifiers.By default constructor will use the same modifiers mentioned in class.