WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Software Engineering · Basic · question 13 of 100

Explain the concept of encapsulation in object-oriented programming and provide an example.?

📕 Buy this interview preparation book: 100 Software Engineering questions & answers — PDF + EPUB for $5

Encapsulation is one of the four fundamental principles of object-oriented programming (OOP). It refers to the practice of bundling an object’s data and methods into a single unit, and restricting access to the object’s internal data from outside the unit.

In simpler terms, encapsulation is like a protective shell around an object, which prevents unauthorized access to its internal data. This allows the object to maintain its integrity and ensures that its behavior is consistent and predictable.

An example of encapsulation is a bank account class. The class would have private data members such as account number, balance, and account holder name. These data members would not be directly accessible from outside the class. Instead, the class would have public member functions such as deposit, withdraw, and getbalance, which manipulate the private data members based on the user’s input.

Here is the implementation of the bank account class in Python:

class BankAccount:
    def __init__(self, acc_num, acc_holder):
        self.__acc_num = acc_num
        self.__acc_holder = acc_holder
        self.__balance = 0
    
    def deposit(self, amount):
        self.__balance += amount
    
    def withdraw(self, amount):
        if amount <= self.__balance:
            self.__balance -= amount
        else:
            print("Insufficient balance")
    
    def getbalance(self):
        return self.__balance

In this implementation, the private data members such as __acc_num, __acc_holder, and __balance are prefixed with double underscores to indicate that they are not supposed to be accessed directly from outside the class. Instead, the class provides public member functions like deposit(), withdraw(), and getbalance(), which allow the user to manipulate and access the private data members in a controlled and safe manner.

By encapsulating the bank account’s data and methods, we ensure that the user cannot modify the account number or the account holder’s name directly, and can only perform operations such as depositing, withdrawing, and checking the account balance in a controlled way. This makes the bank account class more secure, robust, and predictable, and reduces the risk of bugs and security breaches.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Software Engineering interview — then scores it.
📞 Practice Software Engineering — free 15 min
📕 Buy this interview preparation book: 100 Software Engineering questions & answers — PDF + EPUB for $5

All 100 Software Engineering questions · All topics