Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Design a Class with Controlled Read and Write Access

Implement a BankAccount class that demonstrates encapsulation: private attributes, read-only properties, validated setters and controlled write operations.

Python practice30 minEncapsulation & Access PatternsAdvancedLast updated April 8, 2026

Problem statement

Design a BankAccount class that models a simple bank account while demonstrating encapsulation and controlled access patterns. The class must: - Keep the account balance private using name-mangling (double-underscore) so it cannot be directly accessed as an attribute like account.__balance. - Expose a read-only property account_holder for the owner's name. - Expose a read-only property balance for the current balance. - Expose a readable/writable overdraft_limit attribute with validation (must be >= 0). - Allow setting or changing the account PIN via the set_pin(new_pin, old_pin=None) method. If a PIN already exists, old_pin must match; if none exists old_pin may be omitted. - Provide deposit(amount) which increases the balance and returns the new balance. Negative or zero deposits must be rejected and return False. - Provide withdraw(amount, pin) which only succeeds if the provided pin matches and the amount is <= balance + overdraft_limit. On success it deducts the amount and returns the new balance; on failure it returns False. - Provide change_holder(new_name, pin) which updates the account holder only when the correct pin is provided; returns True on success, False otherwise. All operations should validate inputs and fail gracefully (return False) rather than raising exceptions for common invalid usage (e.g., negative deposit, wrong pin). Use Python properties and name-mangling to demonstrate encapsulation.

Task

Create a class that uses name-mangled private attributes, properties for controlled read access, validated setters for write access, and methods that enforce access rules (e.g., PIN checks).

Examples

Basic deposit and balance retrieval

Input

acct = BankAccount('Alice', 100, '1234') acct.deposit(50) acct.balance

Output

150

deposit(50) increases balance to 150 and balance property returns the new balance (150).

Input format

You will instantiate BankAccount(...) and call its methods or access properties. Tests are expressions that evaluate to a return value.

Output format

Methods return values (numbers, strings or False) or properties return current values. The test harness compares the string form of the returned value.

Constraints

Do not expose the raw balance or PIN as public attributes. Use name-mangling for the internal balance (e.g., self.__balance). Overdraft limit must be non-negative. Methods should return False on invalid operations rather than raising errors for typical invalid inputs.

Samples

Sample 1

Input

acct = BankAccount('Bob', 200, '0000', overdraft_limit=50) acct.withdraw(230, '0000') acct.balance

Output

-30

Withdraw 230 from 200 with 50 overdraft allowed leaves -30.