Chapter 1: Problem 28
What is encapsulation?
Short Answer
Expert verified
Provide an example.
Answer: Encapsulation is a core concept in object-oriented programming (OOP) that refers to the bundling of data (attributes) and methods (functions) that operate on the data within a single unit, called a class. It is important because it promotes data hiding, better code organization, and code reusability through inheritance. A simple example of encapsulation is a 'BankAccount' class in Python, where the balance attribute is private and can only be manipulated through methods like deposit, withdraw, and get_balance defined within the class. This ensures data protection and prevents unauthorized access.