Interview Questions, Answers and Tutorials

Access specifiers: public, private, and protected

Access specifiers: public, private, and protected

Access specifiers in Python are like the locks on different doors in a big house. They decide who can go where and do what. Imagine you’re in a house with three types of doors: Public, Private, and Protected. Each door has its own rules about who can use it. Let’s explore these doors and their rules in Python!

1. Public Access Specifier:

Description: Public doors are like the main entrance of a house. Everyone can use them freely without any restrictions.

In Python: In Python, when you create a variable or a method without specifying any access specifier, it becomes public by default. That means anyone can access it from anywhere.

Example:

class House:
    def __init__(self):
        self.public_variable = 10

    def public_method(self):
        return "This is a public method!"
        
# Outside the class
my_house = House()
print(my_house.public_variable)  # Output: 10
print(my_house.public_method())  # Output: This is a public method!

2. Private Access Specifier:

Description: Private doors are like secret passages in the house. Only certain people, like the family members living in the house, can use them. Others from outside can’t see or use them.

In Python: In Python, we use a double underscore (__) prefix to make variables and methods private. This means they can only be accessed from within the class.

Example:

class House:
    def __init__(self):
        self.__private_variable = 20

    def __private_method(self):
        return "This is a private method!"

# Outside the class
my_house = House()
# Trying to access private variables/methods directly will raise an error
print(my_house.__private_variable)  # This will raise an AttributeError
print(my_house.__private_method())  # This will raise an AttributeError

3. Protected Access Specifier:

Description: Protected doors are like doors with a code. People who know the code (like friends or trusted people) can access them, but strangers can’t.

In Python: In Python, we use a single underscore (_) prefix to make variables and methods protected. This indicates that they are meant to be accessed only within the class or its subclasses.

Example:

class House:
    def __init__(self):
        self._protected_variable = 30

    def _protected_method(self):
        return "This is a protected method!"

# Outside the class
my_house = House()
# You can access protected variables/methods, but it's a signal that they're intended for internal use
print(my_house._protected_variable)  # Output: 30
print(my_house._protected_method())  # Output: This is a protected method!

Practice Questions:
  1. Public, Private, or Protected?
    • Identify whether the following variables/methods are public, private, or protected:
class Example:
    def __init__(self):
        self.name = "John"
        self._age = 25
        self.__password = "secret123"
        
    def display_info(self):
        print(f"Name: {self.name}, Age: {self._age}")
        
    def __display_password(self):
        print("Password:", self.__password)

  1. Accessing Private Members:
    • Write a Python program to demonstrate that private members of a class cannot be accessed from outside the class.

Solutions:
  1. Question 1:
    • self.name: Public
    • self._age: Protected
    • self.__password: Private
    • display_info(): Public
    • __display_password(): Private

  1. Question 2:
class House:
    def __init__(self):
        self.__private_variable = 20

    def __private_method(self):
        return "This is a private method!"

# Outside the class
my_house = House()
# Trying to access private variables/methods directly will raise an error
print(my_house.__private_variable)  # This will raise an AttributeError
print(my_house.__private_method())  # This will raise an AttributeError

Understanding access specifiers in Python is like knowing who can go where in a big house. Remember, public means everyone can access it, private means only insiders can use it, and protected means it’s like a semi-secret for the family and close friends. Practice makes perfect, so try out the practice questions to strengthen your understanding!