What Is Access Specifier? A Complete Guide for Beginners and Beyond
By Rahul Singh
Updated on Jun 09, 2026 | 8 min read | 4.41K+ views
Share:
Looks like you're browsing from the
United StatesSome programs may not be available in your location
Some programs may not be available in your location
Switch to upGrad USAll courses
Certifications
More
By Rahul Singh
Updated on Jun 09, 2026 | 8 min read | 4.41K+ views
Share:
Table of Contents
An access specifier, also known as an access modifier, is a keyword in object-oriented programming that controls the visibility and accessibility of classes, methods, variables, and constructors. It determines which parts of a program can access or modify specific members, helping developers manage code organization and data protection.
By restricting access to certain components, access specifiers support encapsulation, improve security, and make applications easier to maintain. They play a key role in controlling how different classes and objects interact within a software system.
In this guide, you will learn what is access specifier, why it matters, how it works in both Java and C++, and how to use each type correctly.
Ready to go beyond algorithms and start building real-world AI solutions? Explore upGrad’s Artificial Intelligence Courses to learn machine learning, generative AI, and cutting-edge technologies through hands-on projects and practical applications.
Think of it like the locks in a building:
Access specifiers are a core part of encapsulation, one of the four pillars of object-oriented programming. They help you hide implementation details and expose only what needs to be exposed. This keeps your code clean, secure, and easy to maintain.
Without access specifiers, any part of your code could read or modify any variable or method in any class. That creates chaos, especially in large projects. Access specifiers solve this by:
In short, access specifiers are how you build software that is both secure and maintainable.
If you are learning Java, understanding what is access specifier in Java is one of the first steps toward writing well-structured code. Java has four access specifiers:
Access Specifier |
Same Class |
Same Package |
Subclass |
Other Packages |
| public | Yes | Yes | Yes | Yes |
| protected | Yes | Yes | Yes | No |
| Default (no keyword) | Yes | Yes | No | No |
| private | Yes | No | No | No |
The public access specifier makes a member visible everywhere in the program. Any class in any package can access it.
public class Animal {
public String name;
public void speak() {
System.out.println("Animal speaks");
}
}
Use public when you want a method or variable to be freely accessible from anywhere.
Also Read: Difference Between Variable and Constant
The private access specifier is the most restrictive. It limits access to the class where it is declared. No other class can access a private member directly.
public class BankAccount {
private double balance;
public double getBalance() {
return balance;
}
}
This is the most common specifier for instance variables. You keep the data private and provide public getter/setter methods to access it safely.
The protected access specifier allows access within the same package and also in subclasses, even if they are in a different package.
public class Vehicle {
protected int speed;
protected void accelerate() {
speed += 10;
}
}
Also Read: Java Language History: Why Java Is So Popular and Widely Used Today
A common question is: what is the default access specifier in Java? When you do not use any keyword, Java applies the default (also called package-private) access level. This makes the member accessible only within the same package.
class Calculator {
int result; // default access
void compute() {
result = 10 + 20;
}
}
This is useful when you want multiple classes in the same package to share data without exposing it to the outside world.
Also Read: Methods in Java: A Complete Guide from Basics to Advanced
Understanding what is access specifier in C++ is equally important if you are working with C++ projects. C++ uses three primary access specifiers: public, private, and protected.
Access Specifier |
Within Class |
Derived Class |
Outside Class |
| public | Yes | Yes | Yes |
| protected | Yes | Yes | No |
| private | Yes | No | No |
In C++, these specifiers are used inside a class or struct definition, and they apply to all members declared after them until the next specifier appears.
class Car {
public:
string brand;
void showBrand() {
cout << brand;
}
};
Public members are accessible from anywhere outside the class as well.
Also Read: 12 Essential Features of C++: Understanding Its Strengths and Challenges in 2025
By default, all members in a C++ class are private unless explicitly stated otherwise. This is one key difference from Java.
class Car {
private:
int engineCode;
};
A private member in C++ cannot be accessed outside the class, not even by derived classes.
What is protected access specifier in C++? The protected specifier makes members accessible within the class itself and in any class that inherits from it. It is not accessible to unrelated classes or code outside the class hierarchy.
class Vehicle {
protected:
int fuelLevel;
};
class Truck : public Vehicle {
public:
void checkFuel() {
cout << fuelLevel; // Accessible here
}
};
This is particularly useful in inheritance, where you want a subclass to access certain properties of its parent class, but you do not want those properties exposed to the rest of the program. Protected sits right between public and private: shared within the family, hidden from strangers.
Also Read: Best C++ Courses in India to Enroll in 2026
One area that confuses many beginners is how access specifiers behave during inheritance. The access level of a member can change depending on the type of inheritance used.
In Java, a subclass always inherits the public and protected members of its parent class. Private members are not inherited. This is consistent regardless of where the subclass is located.
Also Read: Top Java Courses for 2026 – Developer Approved Picks
C++ has three types of inheritance: public, protected, and private. Each type changes how the base class members are visible in the derived class.
Base Class Member |
Public Inheritance |
Protected Inheritance |
Private Inheritance |
| public | public | protected | private |
| protected | protected | protected | private |
| private | Not inherited | Not inherited | Not inherited |
This gives C++ developers more fine-grained control over how a class hierarchy behaves, but it also requires more care when designing your class structures.
Also Read: 3 Compelling C++ Projects in Github [For Beginners in 2026]
Learning what is access specifier is one thing. Avoiding common pitfalls is another. Here are the mistakes most beginners run into:
Here are some rules that experienced developers follow:
Good access control is a sign of thoughtful design. It shows that you have considered not just what your code does, but how it fits into the larger system.
Also Read: 50 Java Projects With Source Code for Beginners
What is access specifier comes down to a set of keywords that control visibility. In Java, you have four: public, protected, default, and private. In C++, you have three: public, protected, and private, with private being the default inside a class. Each serves a specific purpose, and using them correctly is what separates well-designed code from code that is hard to work with.
As you build more complex programs, you will rely on access specifiers to draw clear boundaries between different parts of your system. Start with the principle of least access: give as little access as needed, and expand only when necessary.
Want personalized guidance on AI and upskilling? Speak with an expert for a free 1:1 counselling session today.
An access specifier is a keyword in object-oriented programming that controls who can access a class, method, or variable. It defines the visibility of code elements and is a key part of encapsulation. Common examples include public, private, and protected.
In Java, when you do not use any access keyword, the default access specifier is called package-private. This means the member is accessible only to classes within the same package. It is not accessible from outside the package, unlike public.
Public allows access from anywhere in the program, while private restricts access to only the class in which the member is declared. Private is used to hide data from external code, while public exposes it to everyone.
The protected access specifier in C++ makes a class member accessible within its own class and in any derived class. It is not accessible from outside the class hierarchy. This is widely used when building class hierarchies where subclasses need to work with parent class data.
In a C++ class, all members are private by default unless a specifier is explicitly mentioned. This is different from a C++ struct, where the default is public. Java and C++ differ here, so understanding the language you are working in matters.
Yes. Constructors can be public, protected, or private in both Java and C++. A private constructor prevents a class from being instantiated from outside, which is a common pattern in Singleton design. A protected constructor allows only subclasses to instantiate the parent.
In Java, when a member is protected, it is accessible to the class itself, all classes in the same package, and all subclasses even in different packages. This makes it useful when building inheritance hierarchies where child classes need to access or override parent class behavior.
No. Access specifiers are a compile-time concept. They tell the compiler what is allowed, but they do not affect how the code runs at runtime. Choosing between public, private, or protected has zero impact on execution speed or memory usage.
In Java, you can widen the access in a subclass but not narrow it. For example, if the parent has a protected method, the child can override it as public but not as private. In C++, overriding rules are different depending on how inheritance is declared.
Encapsulation is about bundling data and methods together while controlling how that data is accessed. Access specifiers are the main tool that makes encapsulation possible. By marking fields as private and methods as public, you control exactly how other code interacts with your class.
Yes, making everything public is considered poor practice. It breaks encapsulation, makes debugging harder, and increases the risk of unintended data changes. The general rule is to use the most restrictive access level that still lets your code work correctly, starting with private and opening up only when needed.
64 articles published
Rahul Singh is an Associate Content Writer at upGrad, with a strong interest in Data Science, Machine Learning, and Artificial Intelligence. He combines technical development skills with data-driven s...