Encapsulation bundles data and methods together and hides internal state from outside access. It's a design principle — access modifiers and getters/setters are the mechanisms that achieve it.
private — external code cannot touch them directlyclass BankAccount {
private String accountHolderName;
private double balance;
public BankAccount(String accountHolderName, double balance) {
this.accountHolderName = accountHolderName;
this.balance = balance;
}
public String getAccountHolderName() { return accountHolderName; }
public void setAccountHolderName(String n) { accountHolderName = n; }
public double getBalance() { return balance; }
public void deposit(double amount) {
if (amount > 0) balance += amount;
else System.out.println("Deposit must be positive.");
}
public void withdraw(double amount) {
if (amount > balance) System.out.println("Insufficient funds.");
else balance -= amount;
}
}
class BankAccount {
private:
string accountHolderName;
double balance;
public:
BankAccount(string n, double b) : accountHolderName(n), balance(b) {}
string getAccountHolderName() { return accountHolderName; }
void setAccountHolderName(string n) { accountHolderName = n; }
double getBalance() { return balance; }
void deposit(double amount) {
if (amount > 0) balance += amount;
else cout << "Deposit must be positive." << endl;
}
void withdraw(double amount) {
if (amount > balance) cout << "Insufficient funds." << endl;
else balance -= amount;
}
};
class BankAccount:
def __init__(self, accountHolderName, balance):
self.__accountHolderName = accountHolderName
self.__balance = balance
def getAccountHolderName(self): return self.__accountHolderName
def setAccountHolderName(self, n): self.__accountHolderName = n
def getBalance(self): return self.__balance
def deposit(self, amount):
if amount > 0: self.__balance += amount
else: print("Deposit must be positive.")
def withdraw(self, amount):
if amount > self.__balance: print("Insufficient funds.")
else: self.__balance -= amount
| Language | Private mechanism | Enforced? |
|---|---|---|
| Java | private keyword |
Strictly — compiler error on direct access |
| C++ | private: access specifier |
Strictly — compiler error on direct access |
| Python | Name mangling __var |
Convention-based — accessible via
_ClassName__var
|
Code trace: What does this print?
Person p = new Person();
p.setAge(25);
System.out.println(p.getAge() + " " + p.name);
Given: name is public, age is private with getter/setter.
In Python, is this valid?
class Safe:
def __init__(self):
self.__secret = 42
s = Safe()
print(s._Safe__secret)
Encapsulation ⇒ bundle data + behavior ; hide internal state
Mechanism vs Principle ⇒ access modifiers + getters/setters are mechanisms, encapsulation is the principle
a) Security ⇒ private data prevents direct external manipulation
b) Maintenance ⇒ change internal implementation without breaking callers
c) Modularity ⇒ related fields and methods bundled into a single unit
d) Debugging ⇒ all access goes through methods, easy to validate inputs
e) Simplicity ⇒ hide internal complexity, expose only clear interface
a) Java / C++ ⇒ private keyword ; strict compile-time enforcement
b) Python ⇒ __var name mangling ; convention-only ; bypassable via
_Class__var
Primary source: Oracle Java Tutorials — Object-Oriented Programming Concepts. Ask me anything that's unclear.
Questions? Ask your agent — you can follow up on any concept, quiz answer, or how this applies to a specific coding problem. Review the Java glossary or syntax quick reference for a compressed overview.