Access Specifiers In JAVA

Hey viewers, welcome to my blog. Today I Will be explaining about Access Specifier in java.

Now you might be having a number of questions in your mind. For example –

  • What are Access, Specifiers?
  • Where are they used? and 
  • How are they used? etc. 

Don’t worry I will be answering all these questions.

Let’s get started,

These specifiers are used to restrict the scope of a class, constructor, variable, method or data member. These are also known as Access Modifiers.

There are different kinds of access specifiers which are as follows:-

  1. Default – No keyword required
  2. Private
  3. Protected
  4. Public

access-modifiers-in-java

  1. Default:

Now let’s understand these access specifiers one by one default: If no access modifier is specified for a class, method or data member, then they act as a default access modifier. They are accessible only within the same package.

Example:- Default Access modifiers2. Private:

Moving forward,this is specified using the private keyword.

Data members or methods declared as private are accessible only within the class in which they are declared. Other class of the same package will not be able to access these members. Class and interfaces cannot be declared private.

Example:- 

3.Protected:

Data members or methods declared as protected are accessible within same package or subclasses in the different package.

Example:- protected access modifiers

  1. Public:

    This is specified using the public keyword.

It has the widest scope among all other access modifiers. Classes, data members or methods declared as the public are accessible from everywhere in the program.

 Example:- public access modifiersI hope this article help you understanding the concept of Access Specifiers in JAVA. 

For more such content subscribe to our YouTube channel –https://www.youtube.com/channel/UCcWR1IezEQynJnAETgK8g2w?sub_confirmation=1 and follow us on facebook – https://www.facebook.com/pristinetechschool 
Happy programming !!  

Leave a Reply

Your email address will not be published. Required fields are marked *