【Python入門】オーバーライドを初心者向けに完全解説!メソッドの上書きと継承の活用
Pythonのオーバーライド(override)は、オブジェクト指向プログラミングにおいて、子クラスが親クラスのメソッドを再定義する重要な機能です。既存の動作を変更したり、機能を拡張したりする際に使用されます。この記事では、オーバーライドの基本から実践的な使い方まで、初心者の方にも分かりやすく解説します。
オーバーライドとは?
オーバーライドとは、継承関係にある子クラスで、親クラスのメソッドを同じ名前で再定義することです。子クラスのオブジェクトがそのメソッドを呼び出したとき、親クラスではなく子クラスで定義されたメソッドが実行されます。
オーバーライドのメリット
- 既存機能のカスタマイズ: 親クラスの動作を変更
- ポリモーフィズムの実現: 同じインターフェースで異なる動作
- 段階的な機能拡張: 基本機能を保ちながら特殊化
- コードの再利用: 共通部分は親クラスを活用
基本的なオーバーライドの使い方
1. 最もシンプルなオーバーライド
class Animal:
def speak(self):
print("何かの動物が鳴いています")
class Dog(Animal):
def speak(self): # メソッドをオーバーライド
print("ワンワン!")
class Cat(Animal):
def speak(self): # メソッドをオーバーライド
print("ニャー!")
# 使用例
animal = Animal()
dog = Dog()
cat = Cat()
animal.speak() # 何かの動物が鳴いています
dog.speak() # ワンワン!
cat.speak() # ニャー!
2. 親クラスの機能を拡張するオーバーライド
class Vehicle:
def __init__(self, brand):
self.brand = brand
def start(self):
print(f"{self.brand}のエンジンを始動します")
class Car(Vehicle):
def start(self): # オーバーライドして機能拡張
super().start() # 親クラスの処理を実行
print("シートベルトを確認してください")
class Motorcycle(Vehicle):
def start(self): # 完全に異なる動作に変更
print(f"{self.brand}のバイクのエンジンをキックで始動")
car = Car("トヨタ")
motorcycle = Motorcycle("ホンダ")
car.start()
# トヨタのエンジンを始動します
# シートベルトを確認してください
motorcycle.start()
# ホンダのバイクのエンジンをキックで始動
3. 戻り値を変更するオーバーライド
class Shape:
def area(self):
return 0
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self): # 具体的な計算を実装
return self.width * self.height
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self): # 具体的な計算を実装
return 3.14159 * self.radius ** 2
rect = Rectangle(10, 5)
circle = Circle(3)
print(rect.area()) # 50
print(circle.area()) # 28.27
実践的なオーバーライドの例
1. ファイル処理システム
class FileProcessor:
def __init__(self, filename):
self.filename = filename
def process(self):
print(f"{self.filename}を処理中...")
return "処理完了"
def validate(self):
return self.filename.endswith('.txt')
class ImageProcessor(FileProcessor):
def process(self): # 画像専用の処理にオーバーライド
print(f"画像ファイル{self.filename}を処理中...")
print("- リサイズ処理")
print("- フィルター適用")
return "画像処理完了"
def validate(self): # 画像ファイル用の検証にオーバーライド
return self.filename.lower().endswith(('.jpg', '.png', '.gif'))
class VideoProcessor(FileProcessor):
def process(self): # 動画専用の処理にオーバーライド
print(f"動画ファイル{self.filename}を処理中...")
print("- エンコーディング")
print("- 音声同期")
return "動画処理完了"
def validate(self): # 動画ファイル用の検証にオーバーライド
return self.filename.lower().endswith(('.mp4', '.avi', '.mov'))
# 使用例
text_processor = FileProcessor("document.txt")
image_processor = ImageProcessor("photo.jpg")
video_processor = VideoProcessor("movie.mp4")
print(text_processor.validate()) # True
print(image_processor.validate()) # True
print(video_processor.validate()) # True
image_processor.process()
# 画像ファイルphoto.jpgを処理中...
# - リサイズ処理
# - フィルター適用
2. RPGゲームキャラクターシステム
class Character:
def __init__(self, name, hp, attack):
self.name = name
self.hp = hp
self.max_hp = hp
self.attack = attack
self.level = 1
def attack_enemy(self, enemy):
damage = self.attack
print(f"{self.name}が{enemy.name}に{damage}ダメージ!")
enemy.take_damage(damage)
def take_damage(self, damage):
self.hp -= damage
if self.hp < 0:
self.hp = 0
print(f"{self.name}のHP: {self.hp}")
class Warrior(Character):
def __init__(self, name, hp, attack, defense):
super().__init__(name, hp, attack)
self.defense = defense
def attack_enemy(self, enemy): # 戦士専用の攻撃方法
damage = self.attack + (self.level * 2)
print(f"{self.name}が剣で斬りつけた!")
print(f"{enemy.name}に{damage}ダメージ!")
enemy.take_damage(damage)
def take_damage(self, damage): # 防御力を考慮したダメージ計算
reduced_damage = max(1, damage - self.defense)
print(f"防御力で{damage - reduced_damage}ダメージ軽減!")
super().take_damage(reduced_damage)
class Mage(Character):
def __init__(self, name, hp, attack, mana):
super().__init__(name, hp, attack)
self.mana = mana
self.max_mana = mana
def attack_enemy(self, enemy): # 魔法使い専用の攻撃方法
if self.mana >= 5:
damage = self.attack * 1.5
self.mana -= 5
print(f"{self.name}が魔法を唱えた!")
print(f"{enemy.name}に{damage}ダメージ!")
enemy.take_damage(damage)
else:
print(f"{self.name}はマナが不足している...")
class Archer(Character):
def __init__(self, name, hp, attack, accuracy):
super().__init__(name, hp, attack)
self.accuracy = accuracy
def attack_enemy(self, enemy): # 弓使い専用の攻撃方法
import random
if random.random() < self.accuracy:
damage = self.attack * 1.2 # クリティカル
print(f"{self.name}の矢が急所に命中!")
print(f"{enemy.name}に{damage}ダメージ!")
enemy.take_damage(damage)
else:
print(f"{self.name}の矢が外れた...")
# バトル例
warrior = Warrior("戦士", 120, 20, 5)
mage = Mage("魔法使い", 80, 25, 50)
archer = Archer("弓使い", 100, 18, 0.8)
# 異なる攻撃方法で同じメソッド名を呼び出し
warrior.attack_enemy(mage)
# 戦士が剣で斬りつけた!
# 魔法使いに22ダメージ!
mage.attack_enemy(warrior)
# 魔法使いが魔法を唱えた!
# 戦士に37.5ダメージ!
3. 計算機クラスの階層
class Calculator:
def calculate(self, a, b, operation):
if operation == '+':
return a + b
elif operation == '-':
return a - b
else:
return 0
def display_result(self, result):
print(f"結果: {result}")
class ScientificCalculator(Calculator):
def calculate(self, a, b, operation): # 科学計算機能を追加
# 基本演算は親クラスを利用
if operation in ['+', '-']:
return super().calculate(a, b, operation)
elif operation == '*':
return a * b
elif operation == '/':
return a / b if b != 0 else float('inf')
elif operation == '**':
return a ** b
else:
return 0
def display_result(self, result): # より詳細な表示
if result == float('inf'):
print("エラー: ゼロ除算")
elif isinstance(result, float) and result.is_integer():
print(f"結果: {int(result)}")
else:
print(f"結果: {result:.2f}")
class ProgrammerCalculator(Calculator):
def calculate(self, a, b, operation): # プログラマー向け機能
if operation in ['+', '-']:
return super().calculate(a, b, operation)
elif operation == '&': # ビットAND
return int(a) & int(b)
elif operation == '|': # ビットOR
return int(a) | int(b)
elif operation == '^': # ビットXOR
return int(a) ^ int(b)
else:
return 0
def display_result(self, result): # 進数表示
print(f"結果: {result} (10進数)")
print(f" {bin(int(result))} (2進数)")
print(f" {hex(int(result))} (16進数)")
# 使用例
basic_calc = Calculator()
scientific_calc = ScientificCalculator()
programmer_calc = ProgrammerCalculator()
# 同じメソッド名でも異なる動作
result1 = scientific_calc.calculate(10, 3, '/')
scientific_calc.display_result(result1)
# 結果: 3.33
result2 = programmer_calc.calculate(10, 3, '&')
programmer_calc.display_result(result2)
# 結果: 2 (10進数)
# 0b10 (2進数)
# 0x2 (16進数)
特殊メソッドのオーバーライド
__str__と__repr__のオーバーライド
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"{self.name}({self.age}歳)"
def __repr__(self):
return f"Person('{self.name}', {self.age})"
class Employee(Person):
def __init__(self, name, age, job_title):
super().__init__(name, age)
self.job_title = job_title
def __str__(self): # より詳細な文字列表現にオーバーライド
return f"{self.name}({self.age}歳) - {self.job_title}"
def __repr__(self): # 開発者向け表現をオーバーライド
return f"Employee('{self.name}', {self.age}, '{self.job_title}')"
person = Person("田中", 30)
employee = Employee("佐藤", 25, "エンジニア")
print(str(person)) # 田中(30歳)
print(str(employee)) # 佐藤(25歳) - エンジニア
print(repr(employee)) # Employee('佐藤', 25, 'エンジニア')
演算子のオーバーライド
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
def __str__(self):
return f"Vector({self.x}, {self.y})"
class Vector3D(Vector):
def __init__(self, x, y, z):
super().__init__(x, y)
self.z = z
def __add__(self, other): # 3D対応の加算をオーバーライド
return Vector3D(
self.x + other.x,
self.y + other.y,
self.z + other.z
)
def __str__(self): # 3D表示にオーバーライド
return f"Vector3D({self.x}, {self.y}, {self.z})"
v1 = Vector3D(1, 2, 3)
v2 = Vector3D(4, 5, 6)
v3 = v1 + v2
print(v3) # Vector3D(5, 7, 9)
ポリモーフィズムの実現
統一されたインターフェース
class PaymentProcessor:
def process_payment(self, amount):
print(f"{amount}円の支払いを処理中...")
return True
class CreditCardProcessor(PaymentProcessor):
def process_payment(self, amount):
print(f"クレジットカードで{amount}円を決済")
print("カード情報を確認中...")
return True
class PayPalProcessor(PaymentProcessor):
def process_payment(self, amount):
print(f"PayPalで{amount}円を決済")
print("PayPalアカウントに接続中...")
return True
class BankTransferProcessor(PaymentProcessor):
def process_payment(self, amount):
print(f"銀行振込で{amount}円を決済")
print("銀行システムと連携中...")
return True
# ポリモーフィズムの活用
def handle_payment(processor, amount):
"""支払い方法に関係なく同じように処理"""
if processor.process_payment(amount):
print("決済完了")
else:
print("決済失敗")
# 使用例
processors = [
CreditCardProcessor(),
PayPalProcessor(),
BankTransferProcessor()
]
for processor in processors:
handle_payment(processor, 1000)
print("-" * 30)
よくある間違いと注意点
1. メソッドシグネチャの不一致
# ❌ 間違い - 引数が異なる
class Parent:
def method(self, a, b):
return a + b
class Child(Parent):
def method(self, a): # 引数の数が違う
return a * 2
# ✅ 正解 - 同じシグネチャを維持
class Child(Parent):
def method(self, a, b): # 引数を合わせる
return (a + b) * 2
2. super()の忘れによる機能の欠如
# ❌ 問題のある例
class Parent:
def __init__(self, name):
self.name = name
self.created_at = "2025-07-29"
class Child(Parent):
def __init__(self, name, age):
# super().__init__(name) # 忘れている
self.age = age
# child = Child("田中", 25)
# print(child.created_at) # AttributeError
# ✅ 正しい例
class Child(Parent):
def __init__(self, name, age):
super().__init__(name) # 親の初期化を呼び出し
self.age = age
3. オーバーライドとオーバーロードの混同
# Pythonにはオーバーロード(同名で引数違い)は存在しない
class Calculator:
def add(self, a, b):
return a + b
# これは上のメソッドを置き換える(オーバーライド)
def add(self, a, b, c): # 前のaddは無効になる
return a + b + c
# より良い方法:デフォルト引数を使用
class Calculator:
def add(self, a, b, c=0):
return a + b + c
オーバーライドのベストプラクティス
1. Liskov置換原則の遵守
# ✅ 良い例 - 親クラスと置き換え可能
class Bird:
def fly(self):
return "飛んでいます"
class Eagle(Bird):
def fly(self): # より具体的だが同じ動作
return "高空を飛んでいます"
# ❌ 悪い例 - 置き換えできない
class Penguin(Bird):
def fly(self):
raise NotImplementedError("ペンギンは飛べません")
2. 適切なドキュメント化
class Animal:
def speak(self):
"""動物の鳴き声を出力する"""
print("何かの音")
class Dog(Animal):
def speak(self):
"""犬の鳴き声を出力する
親クラスのspeak()メソッドをオーバーライドして
犬特有の鳴き声「ワンワン」を出力する
"""
print("ワンワン")
3. テストの充実
def test_polymorphism():
"""ポリモーフィズムのテスト"""
animals = [Dog(), Cat(), Bird()]
for animal in animals:
# 全ての動物で同じメソッドが呼び出せる
animal.speak()
assert hasattr(animal, 'speak')
まとめ
Pythonのオーバーライドは、オブジェクト指向プログラミングにおいて柔軟で拡張性の高いプログラムを作成するための重要な機能です。適切に使用することで、コードの再利用性とポリモーフィズムを実現できます。
重要なポイント
- 子クラスで親クラスのメソッドを再定義
- 同じインターフェースで異なる動作を実現
- super()を使って親クラスの機能も活用
- ポリモーフィズムで統一された処理を実現
- Liskov置換原則を遵守する
まずは簡単なオーバーライドから始めて、徐々に複雑なポリモーフィズムを活用したプログラム設計にチャレンジしてみましょう。実際にコードを書いて練習することで、オーバーライドの概念がしっかりと身に付きます!
■プロンプトだけでオリジナルアプリを開発・公開してみた!!
■AI時代の第一歩!「AI駆動開発コース」はじめました!
テックジム東京本校で先行開始。
■テックジム東京本校
「武田塾」のプログラミング版といえば「テックジム」。
講義動画なし、教科書なし。「進捗管理とコーチング」で効率学習。
より早く、より安く、しかも対面型のプログラミングスクールです。
<短期講習>5日で5万円の「Pythonミニキャンプ」開催中。
<月1開催>放送作家による映像ディレクター養成講座
<オンライン無料>ゼロから始めるPython爆速講座


