Python Programming - Encapsulation - Discussion

Discussion Forum : Encapsulation - General Questions (Q.No. 10)
10.
Consider the following Python code:
class BankAccount:
    def __init__(self, balance):
        self.__balance = balance

    def get_balance(self):
        return self.__balance

    def deposit(self, amount):
        self.__balance += amount

    def withdraw(self, amount):
        if amount <= self.__balance:
            self.__balance -= amount
            return True
        else:
            return False
What concept of encapsulation is demonstrated in this code?
Public access specifier
Private access specifier
Protected access specifier
Global variable
Answer: Option
Explanation:
The double underscore prefix (__) in the variable __balance indicates that it is a private variable, demonstrating encapsulation by hiding the implementation details.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.