Skip to content

Python! Inheritance | Lectures 17

Lecture 17 Inheritance More OOP Concepts

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 Cat and Animal.

๐Ÿง  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 Dog It is a type of Animal.
  • A Car It is a type of Vehicle.

We say:

  • Dog is a child class (or subclass) of Animal.
  • Animal is 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 name from Animal
  • A new wingspan attribute
Lecture 17 Inheritance More OOP Concepts
Lecture 17 Inheritance More OOP Concepts

๐Ÿงช Try It Yourself!

Write a program with:

  • A parent class Vehicle with method move()
  • Child classes Car, Boat, and Plane that override move()

๐Ÿ’ก 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, Archer Each 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

Najeeb Alam

Najeeb Alam

Technical writer specializes in developer, Blogging and Online Journalism. I have been working in this field for the last 20 years.

Leave a Reply

Your email address will not be published. Required fields are marked *