Let’s Start Python Programming for Beginners to Advanced. Inheritance & More OOP Concepts, Lectures 17
๐ Lecture 17: Inheritance & More OOP Concepts
๐ฏ Goal
By the end of this lecture, you will:
- Understand what inheritance is and how it works.
- Be able to create a child class that inherits from a parent class.
- Learn how to override methods and use
super(). - Practice building related classes like
CatandAnimal.
๐ง What Is Inheritance?
In real life, children often inherit traits from their parents, like eye color or height.
In programming, inheritance means one class can inherit attributes and methods from another class.
This helps us reuse code and build more specific classes based on more general ones.
Example:
- A
DogIt is a type ofAnimal. - A
CarIt is a type ofVehicle.
We say:
Dogis a child class (or subclass) ofAnimal.Animalis the parent class (or superclass).
๐ง How Inheritance Works in Python
Letโs define a simple Animal class:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print("I make a sound.")Now let’s make a Dog A class that inherits from Animal:
class Dog(Animal):
def bark(self):
print("Woof!")Now Dog has everything Animal has โ plus its own new method!
Try It:
my_dog = Dog("Buddy")
my_dog.speak() # From Animal
my_dog.bark() # From Dog๐ Output:
I make a sound.
Woof!๐งฌ Overriding Methods
Sometimes we want the child class to do something different than the parent.
We can override a method by redefining it in the child class.
class Cat(Animal):
def speak(self):
print("Meow!")Now when a Cat speaks, it says "Meow!" instead of "I make a sound."
Try It:
my_cat = Cat("Whiskers")
my_cat.speak() # Now prints "Meow!"๐ Using super() โ Calling the Parent Class
If you still want to use the parentsโ version of a method, use super().
This is useful for adding behavior without completely replacing it.
Example:
class Bird(Animal):
def __init__(self, name, wingspan):
super().__init__(name) # Call parent's __init__
self.wingspan = wingspan
def speak(self):
print("Chirp!")
def fly(self):
print(f"{self.name} is flying!")Now Bird has both:
- The
namefromAnimal - A new
wingspanattribute

๐งช Try It Yourself!
Write a program with:
- A parent class
Vehiclewith methodmove() - Child classes
Car,Boat, andPlanethat overridemove()
๐ก Sample Code:
class Vehicle:
def move(self):
print("Moving...")
class Car(Vehicle):
def move(self):
print("Driving on roads.")
class Boat(Vehicle):
def move(self):
print("Sailing on water.")
class Plane(Vehicle):
def move(self):
print("Flying in the sky.")
vehicles = [Car(), Boat(), Plane()]
for v in vehicles:
v.move()๐ Output:
Driving on roads.
Sailing on water.
Flying in the sky.๐จ Challenge (Optional)
Make a game with different types of characters:
- Base class:
Character - Subclasses:
Warrior,Mage,ArcherEach should have a unique attack message.
Example:
class Character:
def attack(self):
print("Character attacks!")
class Warrior(Character):
def attack(self):
print("โ๏ธ Warrior swings sword!")
class Mage(Character):
def attack(self):
print("๐ฎ Mage casts fireball!")
class Archer(Character):
def attack(self):
print("๐น Archer shoots arrow!")
characters = [Warrior(), Mage(), Archer()]
for c in characters:
c.attack()๐ง Kids Corner
๐ง Imagine you have a robot parent and a robot kid.
The kid robot knows everything the parent knows โ plus some cool new tricks of its own!
๐ค Robot Kid says:
I learned from my robot parent โ but I added my own moves too!๐ Summary
- Use inheritance to create child classes that reuse parent code.
- Override methods to change behavior in the child class.
- Use
super()to call the parentโs version of a method. - This helps organize your code and avoid repetition.
Stay Updated
If you found this information useful, donโt forget to bookmark this page and Share and leave your feedback in the comment section below.
Python Programming For Beginners To Advanced
Python Programming For Beginners! Lecture 1
Python Programming! Variables and Data Types | Lectures 2
Python Programming! Input And Output | Lectures 3
Python Programming! Operators in Python Lectures 4
Python Programming! Conditional Statements Lectures 5
Python! Loops For and While | Lectures 6
Python! Building A Mini Game | Lectures 7
Python! Functions | Lectures 8
Python! Storing Multiple Values | Lectures 9
Python! Tuples Sets And Dictionaries | Lectures 10
Python! Working with Strings | Lectures 11
Python Programming! File Handling | Lectures 12
Python Programming! Error Handling | Lectures 13
Python! Modules And Packages | Lectures 14
Python Programming! File Handling | Lectures 15
Python! Classes And Objects | Lectures 16
Python! Inheritance | Lectures 17
