Inheritance
What is Inheritance?
Inheritance is when a class (child/subclass) takes on properties and behaviors (attributes and methods) from another class (parent/superclass). This helps you reuse code and organize programs efficiently.
Basic Example
class Vehicle:
def move(self):
print("Vehicle is moving")
class Car(Vehicle): # Car inherits from Vehicle
def honk(self):
print("Car honks: Beep beep!")
my_car = Car()
my_car.move() # Output: Vehicle is moving (inherited)
my_car.honk() # Output: Car honks: Beep beep!
Real World Use Cases
Banking Applications: A
BankAccount
base class can be inherited bySavingsAccount
andCheckingAccount
subclasses, each with specific features.E-commerce Systems: A general
Product
class can be the parent forElectronics
,Clothing
, andBooks
, each with additional properties.Game Development: A generic
Character
class can be inherited byHero
andEnemy
, sharing some behaviors but also having unique ones.
Key Points
Reduces code duplication.
Easier to extend and maintain code.
"is-a" relationship: a Car "is a" Vehicle.
Last updated
Was this helpful?