What are the access specifiers?
Access specifiers (also known as access modifiers) are keywords used in object-oriented programming (OOP) to set the visibility or accessibility of classes, methods, and attributes. They control how and where the members (variables and methods) of a class can be accessed, ensuring that the internal workings of an object are protected and that interactions with the object follow a controlled interface.
There are typically three main access specifiers used in most OOP languages:
1. Public
- Description: The
public
access specifier allows the member (method or variable) to be accessible from anywhere in the program, whether it’s within the class, subclass, or from other classes entirely. - Usage: It's used when you want the attribute or method to be freely accessible without any restrictions.
Example:
class Car: def __init__(self, make, model): self.make = make # Public attribute self.model = model # Public attribute def start_engine(self): # Public method print("Engine started.") # Usage car = Car("Toyota", "Corolla") print(car.make) # Accessible from outside the class car.start_engine() # Accessible from outside the class
In this example, the make
and model
attributes, along with the start_engine()
method, are public and can be accessed directly by creating an instance of the Car
class.
2. Private
- Description: The
private
access specifier restricts access to the members of a class. These members can only be accessed within the same class and are hidden from outside. In Python, this is typically represented by prefixing the member name with two underscores (__
), but in other languages like Java or C++, you can use theprivate
keyword. - Usage: It's used when you want to protect the data from external manipulation and only allow access through methods (like getters and setters).
Example:
class Car: def __init__(self, make, model): self.__make = make # Private attribute self.__model = model # Private attribute def display_info(self): # Public method print(f"Car make: {self.__make}, Model: {self.__model}") # Usage car = Car("Toyota", "Corolla") car.display_info() # Works fine # print(car.__make) # Raises an error because __make is private
In this example, the __make
and __model
attributes are private and cannot be accessed directly from outside the class.
3. Protected
- Description: The
protected
access specifier allows the member to be accessible within the class and its subclasses (derived classes), but not from outside the class hierarchy. In Python, this is typically represented by a single underscore (_
), while in languages like Java and C++, you can use theprotected
keyword. - Usage: It's used when you want to allow subclasses to access and modify these members, but keep them hidden from the outside world.
Example:
class Car: def __init__(self, make, model): self._make = make # Protected attribute self._model = model # Protected attribute class ElectricCar(Car): def display_info(self): print(f"Electric car make: {self._make}, Model: {self._model}") # Usage car = ElectricCar("Tesla", "Model S") car.display_info() # Works fine, as _make and _model are accessible in the subclass
In this example, the _make
and _model
attributes are protected and can be accessed by the subclass ElectricCar
, but cannot be accessed directly from outside the class.
Summary of Access Specifiers
Access Specifier | Accessibility | Use Case |
---|---|---|
Public | Accessible from anywhere (inside and outside the class) | Use when you want members to be freely accessible. |
Private | Accessible only within the class | Use when you want to protect data from external modification. |
Protected | Accessible within the class and its subclasses | Use when members need to be accessible by subclasses but not from outside the class hierarchy. |
Recommended Courses
To deepen your understanding of access specifiers and other OOP concepts, consider enrolling in the following courses from DesignGurus.io:
- Grokking Data Structures & Algorithms for Coding Interviews
- Grokking the Coding Interview: Patterns for Coding Questions
- Grokking the System Design Interview
These courses will provide you with a deeper understanding of OOP principles and prepare you for your technical interviews.
GET YOUR FREE
Coding Questions Catalog