access control modifiers

What are access control modifiers? | Java Advanced | Part – 2

My mom has given me strict instructions to not step outside the room during the study hours.

She has restricted my access to the rest of the house.

And wants me to focus on studies completely.

I wonder if she knows about access control modifiers.

Access control modifiers

Access control modifiers are keywords which are used to restrict the scope of the class, data members, methods or constructors.

Scope defines from where these entities can be accessed and where they cannot.

They specify which classes can access a given class and its fields, constructors and methods.

*We will be learning about constructors later in this course series post.

There are 4 access control modifiers provided by Java :

Access Control Modifiers
  1. Default
  2. Public
  3. Private
  4. Protected

Default

Default

When no access specifier is declared, it is said to have the default access control modifier.

This is true for class, methods, and data members.

Entities having default access specifier are accessible in the same package.

Package is a group of classes and methods inside the same directory. Java uses directories to define packages.

To have a default access specifier, we don’t need to specify anything explicitly.

 class SomeClass{

//body

}

Here, class SomeClass is not declared using any access specifier, thus its access control modifier is default.

This class can be accessed in the same package only, meaning that another class in the same folder could access it, while one in a different folder couldn’t.

Public

Public

The public access control modifier is accessible everywhere.

It has the widest scope among the four.

To define an entity as public we use the keyword public.

When defined as public the entity is accessible outside the package also.

public class ThisClass{

public int thisNum = 10;

}

Here, class ThisClass is declared using the keyword public, thus its access control modifier is public.

Similarly, the data member thisNum is also declared as public.

The class ThisClass and data member thisNum can be accessed everywhere.

If thisNum wasn’t public, a class could access the contents of ThisClass, but not thisNum!

Private

Private

The private access control modifier is accessible only within the same class.

The data and functions declared as private are only accessible within the same class.

It has the least scope among the four.

To define an entity as private we use the keyword private.

 class ThisClass {

private int thisNum = 10;

Here, the data member thisNum is declared using the keyword private, thus its access control modifier is private.

Thus, they can be accessed only within the class ThisClass.

Protected

Protected

The protected access control modifier is accessible within the same package as well as outside but only using inheritance.

Only the methods, data members and constructors can be declared as protected.

Classes cannot be declared as protected.

*We will be learning about inheritance further in the course series post.

To define an entity as protected we use the keyword protected.

 class ThisClass{

protected int thisNum = 10;

}

Here, the data member thisNum is declared using the keyword protected, thus its access control modifier is protected.

Thus, it can be accessed within the same package and outside but only when some class inherits the class ThisClass.

To Summarize

  • Access control modifiers are keywords which specify which classes can access a given class and its fields, constructors and methods.
  • When no access specifier is declared, it is said to have the default access control modifier. Entities having default access specifier are accessible in the same package.
  • The public access control modifier is accessible everywhere.
  • The private access control modifier is accessible only within the same class.
  • The protected access control modifier is accessible within the same package as well as outside but only using inheritance.

Recommended Boks For Advanced Java



Leave a Comment

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

PYQ of History UGC NET UGC NET Mathematical Reasoning and Aptitude ICT (Digital Initiatives) | UGC NET | paper – 1 The Scope of Information and Communication Technology(ICT) PYQ of People, Development, and Environment Top 30 PYQ of HINDI | UGC NET – 2023 Top 30 PYQ of Teaching Aptitude PYQ of Research Aptitude | NTA UGC NET | Paper 1 | Part 1 UGC NET Paper 1 Syllabus | Updated | 2023 Types of Research | Research Aptitude | nta ugc net | UGC NET 2023