カプセル化とは?オブジェクト指向プログラミングの基本概念を徹底解説【2025年版】
カプセル化の基本概念
カプセル化(Encapsulation)は、オブジェクト指向プログラミングの基本原則の一つです。データ(属性)とそのデータを操作するメソッド(処理)を一つのクラス内にまとめ、外部からの不正なアクセスを制限する仕組みです。これにより、コードの安全性、保守性、再利用性が大幅に向上します。
カプセル化の主な目的
データの保護
- 外部からの直接的なデータアクセスを防ぐ
- データの整合性を保つ
- 予期しない値の変更を防止
実装の隠蔽
- 内部実装の詳細を外部に公開しない
- インターフェースの単純化
- 変更による影響範囲の限定
コードの保守性向上
- 機能の変更が他の部分に与える影響を最小化
- バグの発生源を特定しやすくする
- テストの容易性向上
アクセス修飾子による制御
public(公開)
public class BankAccount {
public String accountNumber; // 外部から直接アクセス可能
public void displayInfo() { // 外部から呼び出し可能
System.out.println("口座番号: " + accountNumber);
}
}
private(非公開)
public class BankAccount {
private double balance; // 外部から直接アクセス不可
private void validateAmount(double amount) { // 内部でのみ使用
if (amount < 0) throw new IllegalArgumentException("負の値は無効");
}
}
protected(継承クラスからアクセス可能)
public class Account {
protected String accountType; // 継承クラスからアクセス可能
protected void setAccountType(String type) {
this.accountType = type;
}
}
各言語でのカプセル化実装
Java での実装例
public class Student {
private String name;
private int age;
private double gpa;
// コンストラクタ
public Student(String name, int age) {
setName(name);
setAge(age);
}
// Getter メソッド
public String getName() { return name; }
public int getAge() { return age; }
// Setter メソッド(検証ロジック付き)
public void setName(String name) {
if (name == null || name.trim().isEmpty()) {
throw new IllegalArgumentException("名前は必須です");
}
this.name = name;
}
public void setAge(int age) {
if (age < 0 || age > 150) {
throw new IllegalArgumentException("年齢は0-150の範囲で入力してください");
}
this.age = age;
}
}
Python での実装例
class BankAccount:
def __init__(self, account_number, initial_balance=0):
self._account_number = account_number # プロテクテッド
self.__balance = initial_balance # プライベート
@property
def balance(self):
return self.__balance
@property
def account_number(self):
return self._account_number
def deposit(self, amount):
if amount > 0:
self.__balance += amount
return True
return False
def withdraw(self, amount):
if 0 < amount <= self.__balance:
self.__balance -= amount
return True
return False
C++ での実装例
class Rectangle {
private:
double width, height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
// Getter
double getWidth() const { return width; }
double getHeight() const { return height; }
// Setter
void setWidth(double w) {
if (w > 0) width = w;
}
void setHeight(double h) {
if (h > 0) height = h;
}
double getArea() const {
return width * height;
}
};
JavaScript での実装例
class Car {
#speed = 0; // プライベートフィールド
#maxSpeed;
constructor(maxSpeed) {
this.#maxSpeed = maxSpeed;
}
get speed() {
return this.#speed;
}
accelerate(increment) {
const newSpeed = this.#speed + increment;
if (newSpeed <= this.#maxSpeed) {
this.#speed = newSpeed;
}
}
brake(decrement) {
const newSpeed = this.#speed - decrement;
this.#speed = Math.max(0, newSpeed);
}
}
C# での実装例
public class Employee {
private string name;
private decimal salary;
public string Name {
get { return name; }
set {
if (!string.IsNullOrEmpty(value))
name = value;
}
}
public decimal Salary {
get { return salary; }
set {
if (value >= 0)
salary = value;
}
}
public void GiveRaise(decimal amount) {
if (amount > 0) {
salary += amount;
}
}
}
カプセル化のデザインパターン
Getter/Setter パターン
public class Temperature {
private double celsius;
public double getCelsius() {
return celsius;
}
public void setCelsius(double celsius) {
if (celsius < -273.15) {
throw new IllegalArgumentException("絶対零度以下は設定できません");
}
this.celsius = celsius;
}
public double getFahrenheit() {
return celsius * 9.0 / 5.0 + 32;
}
}
Builder パターンでのカプセル化
public class User {
private final String name;
private final String email;
private final int age;
private User(Builder builder) {
this.name = builder.name;
this.email = builder.email;
this.age = builder.age;
}
public static class Builder {
private String name;
private String email;
private int age;
public Builder setName(String name) {
this.name = name;
return this;
}
public Builder setEmail(String email) {
this.email = email;
return this;
}
public User build() {
return new User(this);
}
}
}
Factory パターンでのカプセル化
public class DatabaseConnection {
private String connectionString;
private boolean isConnected;
private DatabaseConnection(String connectionString) {
this.connectionString = connectionString;
}
public static DatabaseConnection create(String host, String database) {
String connectionString = "jdbc:mysql://" + host + "/" + database;
return new DatabaseConnection(connectionString);
}
public boolean connect() {
// 接続ロジック
isConnected = true;
return isConnected;
}
}
カプセル化のメリット
データの整合性保証
カプセル化により、データの設定時に検証ロジックを組み込むことができます。これにより、オブジェクトの状態が常に有効であることを保証できます。
public class Circle {
private double radius;
public void setRadius(double radius) {
if (radius <= 0) {
throw new IllegalArgumentException("半径は正の値である必要があります");
}
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
}
変更への耐性
内部実装を変更しても、外部インターフェースが変わらなければ、他のコードに影響を与えません。
// 内部実装変更前
public class Calculator {
public double add(double a, double b) {
return a + b; // 単純な加算
}
}
// 内部実装変更後(ログ機能追加)
public class Calculator {
private Logger logger = new Logger();
public double add(double a, double b) {
logger.log("計算実行: " + a + " + " + b);
return a + b;
}
}
コードの再利用性
適切にカプセル化されたクラスは、他のプロジェクトでも容易に再利用できます。
public class EmailValidator {
private static final String EMAIL_PATTERN =
"^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$";
public static boolean isValid(String email) {
return email != null && email.matches(EMAIL_PATTERN);
}
}
カプセル化の注意点とアンチパターン
過度なカプセル化
// 悪い例:不必要に複雑
public class Point {
private int x, y;
public int getX() { return x; }
public void setX(int x) { this.x = x; }
public int getY() { return y; }
public void setY(int y) { this.y = y; }
}
// 良い例:シンプルな構造
public class Point {
public final int x, y; // 不変オブジェクトとして設計
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
Getter/Setter の濫用
// 悪い例:単純なデータホルダー
public class Product {
private String name;
private double price;
// 単純なGetter/Setterのみ
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
// 良い例:ビジネスロジックを含む
public class Product {
private String name;
private double price;
public void applyDiscount(double percentage) {
if (percentage > 0 && percentage <= 100) {
price *= (1 - percentage / 100);
}
}
public boolean isExpensive() {
return price > 1000;
}
}
カプセル化とテスト
テスト可能な設計
public class OrderProcessor {
private PaymentService paymentService;
private InventoryService inventoryService;
// 依存性注入でテスト可能に
public OrderProcessor(PaymentService paymentService,
InventoryService inventoryService) {
this.paymentService = paymentService;
this.inventoryService = inventoryService;
}
public boolean processOrder(Order order) {
if (!inventoryService.isAvailable(order.getProductId())) {
return false;
}
return paymentService.processPayment(order.getAmount());
}
}
モックを使用したユニットテスト
@Test
public void testOrderProcessing() {
// Arrange
PaymentService mockPayment = Mockito.mock(PaymentService.class);
InventoryService mockInventory = Mockito.mock(InventoryService.class);
when(mockInventory.isAvailable(anyString())).thenReturn(true);
when(mockPayment.processPayment(anyDouble())).thenReturn(true);
OrderProcessor processor = new OrderProcessor(mockPayment, mockInventory);
Order order = new Order("product1", 100.0);
// Act
boolean result = processor.processOrder(order);
// Assert
assertTrue(result);
}
カプセル化のベストプラクティス
1. 最小権限の原則
可能な限り制限的なアクセス修飾子を使用します。
public class BankAccount {
private double balance; // 外部からアクセス不可
private String accountNumber; // 外部からアクセス不可
public double getBalance() { // 必要な情報のみ公開
return balance;
}
}
2. 不変オブジェクトの活用
public final class Money {
private final BigDecimal amount;
private final String currency;
public Money(BigDecimal amount, String currency) {
this.amount = amount;
this.currency = currency;
}
public Money add(Money other) {
if (!currency.equals(other.currency)) {
throw new IllegalArgumentException("通貨が異なります");
}
return new Money(amount.add(other.amount), currency);
}
}
3. インターフェースの活用
public interface Shape {
double getArea();
double getPerimeter();
}
public class Rectangle implements Shape {
private double width, height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
@Override
public double getPerimeter() {
return 2 * (width + height);
}
}
カプセル化と他のOOP原則との関係
継承との組み合わせ
public abstract class Animal {
protected String name; // 継承クラスからアクセス可能
private int age; // このクラス内でのみアクセス可能
protected void setAge(int age) {
if (age >= 0) this.age = age;
}
public abstract void makeSound(); // 継承クラスで実装必須
}
public class Dog extends Animal {
private String breed;
public Dog(String name, String breed) {
this.name = name; // protectedなのでアクセス可能
this.breed = breed;
}
@Override
public void makeSound() {
System.out.println(name + "がワンワンと鳴いています");
}
}
ポリモーフィズムとの組み合わせ
public abstract class Vehicle {
protected double speed;
public abstract void accelerate();
public abstract void brake();
public double getSpeed() {
return speed;
}
}
public class Car extends Vehicle {
@Override
public void accelerate() {
speed = Math.min(speed + 10, 120); // 最高速度120km/h
}
@Override
public void brake() {
speed = Math.max(speed - 15, 0);
}
}
実際の開発でのカプセル化活用
Web開発での例
@RestController
public class UserController {
private UserService userService; // 依存性注入
@PostMapping("/users")
public ResponseEntity<User> createUser(@RequestBody UserDto userDto) {
try {
User user = userService.createUser(userDto);
return ResponseEntity.ok(user);
} catch (ValidationException e) {
return ResponseEntity.badRequest().build();
}
}
}
public class UserService {
private UserRepository userRepository;
public User createUser(UserDto dto) {
validateUserData(dto); // プライベートメソッドで検証
User user = new User(dto.getName(), dto.getEmail());
return userRepository.save(user);
}
private void validateUserData(UserDto dto) {
if (dto.getName() == null || dto.getName().trim().isEmpty()) {
throw new ValidationException("名前は必須です");
}
}
}
ゲーム開発での例
public class Player {
private int health;
private int maxHealth;
private Weapon weapon;
public Player(int maxHealth) {
this.maxHealth = maxHealth;
this.health = maxHealth;
}
public void takeDamage(int damage) {
health = Math.max(0, health - damage);
if (health == 0) {
triggerGameOver();
}
}
public void heal(int amount) {
health = Math.min(maxHealth, health + amount);
}
private void triggerGameOver() {
// ゲームオーバー処理
}
}
まとめ
カプセル化は、オブジェクト指向プログラミングの基本原則として、コードの品質と保守性を大幅に向上させる重要な概念です。適切なアクセス修飾子の使用、Getter/Setterパターンの活用、データ検証の実装により、安全で再利用可能なコードを作成できます。
現代のソフトウェア開発では、カプセル化は単なる理論ではなく、実践的なコード品質向上のための必須技術です。マイクロサービス、クリーンアーキテクチャ、テスト駆動開発など、様々な開発手法でもカプセル化の原則が活用されています。
プログラミング初心者から上級者まで、カプセル化の概念を正しく理解し、実践することで、より堅牢で保守性の高いソフトウェアを開発できるようになるでしょう。
■プロンプトだけでオリジナルアプリを開発・公開してみた!!
■AI時代の第一歩!「AI駆動開発コース」はじめました!
テックジム東京本校で先行開始。
■テックジム東京本校
「武田塾」のプログラミング版といえば「テックジム」。
講義動画なし、教科書なし。「進捗管理とコーチング」で効率学習。
より早く、より安く、しかも対面型のプログラミングスクールです。
<短期講習>5日で5万円の「Pythonミニキャンプ」開催中。
<月1開催>放送作家による映像ディレクター養成講座
<オンライン無料>ゼロから始めるPython爆速講座



