Describing Class Structure
Attributes, methods, constructors, encapsulation, dependencies — OOP vocabulary for code reviews
Class vocabulary
- Attributes (fields) — data the class holds; methods — behaviors it exposes
- Instance method → self/this; class method → cls; static method → no self
- __str__ / toString() — human-readable string representation
- Encapsulation — bundle data + behavior; hide internal state behind a public interface
- Dependency injection — pass dependencies via constructor instead of creating them internally
Question 0 of 5
How would you describe this class in one sentence?class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self._balance = balance
def deposit(self, amount): ...
def withdraw(self, amount): ...
def get_balance(self): ...
Represents a bank account with owner, private balance, and deposit/withdraw/get_balance methods. Class description vocabulary:
- attributes (fields/properties) — data the class holds:
owner,_balance - methods — behaviors the class exposes:
deposit(),withdraw(),get_balance() - constructor (__init__ / constructor) — initializes a new instance
- private (underscore prefix) —
_balance— convention for "not intended for external access"
What do "instance method", "class method", and "static method" mean in OOP?
Instance → self/this; class → cls (class state); static → no self/cls (utility). Method type vocabulary:
- Instance method — receives
self(Python) orthis(JS/Java); operates on the specific object; most common - Class method —
@classmethodin Python,staticon a factory method in Java; receivescls; useful for factory methods:BankAccount.from_dict(data) - Static method —
@staticmethod; no access to instance or class; pure utility grouped with the class for organization
self — make it a @staticmethod or move it to a module-level function."A class has: def __str__(self): return f"Account({self.owner})". What is the purpose of this method?
Defines the human-readable string representation — called by print() and str(). Python dunder (magic) method vocabulary:
- __str__ — human-readable string; called by
print(obj)andstr(obj) - __repr__ — developer-readable; should ideally return something that can recreate the object; shown in the REPL
- __eq__ — defines
==comparison between objects - __len__ — defines
len(obj) - __iter__ — makes the object iterable (usable in for loops)
toString() serves the same role as __str__. In JavaScript: toString() and toJSON() (for JSON.stringify). Describing these: "This class overrides toString to display the owner name when printed."What does the term "encapsulation" mean when describing a class in a code review?
Bundling data + behavior and controlling access to internal state. Encapsulation vocabulary in code reviews:
- "Encapsulates the state" — the class owns its data; only its methods change it
- "Breaks encapsulation" — code outside the class directly modifies private attributes
- "Exposes a clean interface" — only public methods are needed; internals can change without affecting callers
- Getter/setter — methods that read/write private attributes with optional validation
account._balance breaks encapsulation. Use get_balance() instead — this lets us add validation or logging later without changing callers."A class diagram shows: UserService depends on UserRepository. In a code review, what does "dependency" mean here?
UserService uses UserRepository — holds a reference and calls its methods. Dependency vocabulary in OOP and architecture:
- Dependency — class A uses class B; A "depends on" B
- Dependency injection (DI) — B is passed to A's constructor instead of A creating B; makes A easier to test
- Tight coupling — A creates B internally; hard to swap B for a different implementation
- Loose coupling — A depends on an interface/abstract type; B can be swapped without changing A