What are the access specifiers?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

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 the private 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 the protected 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 SpecifierAccessibilityUse Case
PublicAccessible from anywhere (inside and outside the class)Use when you want members to be freely accessible.
PrivateAccessible only within the classUse when you want to protect data from external modification.
ProtectedAccessible within the class and its subclassesUse when members need to be accessible by subclasses but not from outside the class hierarchy.

To deepen your understanding of access specifiers and other OOP concepts, consider enrolling in the following courses from DesignGurus.io:

These courses will provide you with a deeper understanding of OOP principles and prepare you for your technical interviews.

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What are the 5 P's of interview?
What are the 5 criteria for system design?
Do I need to prepare for behavioral interview?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.