Python Basics: Object Oriented Programming (OOP)
Introduction to Inheritance
Lesson 3 of 7 • 10 XP
Keep your place in this quest
Log in or sign up for free to subscribe, follow lesson progress, and access more learning content.
Inheritance is one of the core concepts of Object-Oriented Programming (OOP).
It allows one class (called the child or subclass) to inherit the attributes and methods of another class (called the parent or superclass).
This means you can define common behavior and data in a base class, then create more specialized classes that reuse and customize that functionality — without having to rewrite the same code.
Basic Syntax for Inheritance
In Python, inheritance is implemented by specifying the parent class name in parentheses after the child class name.
class ParentClass:
# Attributes and methods of the parent class
class ChildClass(ParentClass):
# Attributes and methods of the child class
````
When a child class inherits from a parent class, it gains:
* All the parent’s **attributes** (variables tied to the object)
* All the parent’s **methods** (functions tied to the object)
* Any **constructor** (`__init__`) the parent class may have
---
# Example: Animals and Sounds
Let’s bring inheritance to life with something we can all relate to — animals.
We’ll start with a general `Animal` class that defines what all animals have in common, and then we’ll create specific types of animals (`Dog` and `Cat`) that share those traits but make different sounds.
Here’s the base class:
```python
class Animal:
def __init__(self, name, age):
self.name = name
self.age = age
def sound(self):
pass # We'll let each specific animal decide its own sound
````
The `Animal` class has:
* An `__init__` method that sets up the `name` and `age` for each animal.
* A `sound()` method that doesn’t do anything yet (`pass` is just a placeholder).
We’ll replace it in each animal type with the sound that species makes.
---
Now let’s create two specific animal classes:
```python
class Dog(Animal):
def sound(self):
print("Woof woof!")
class Cat(Animal):
def sound(self):
print("Meow!")
Here’s what’s happening:
- Both
DogandCatinherit fromAnimal, so they automatically get thenameandageattributes without us having to rewrite that code. - Each one overrides the
sound()method to make its own unique sound.
Creating and Using the Objects
dog = Dog("Rex", 2)
cat = Cat("Mimi", 3)
print(dog.name) # Rex
dog.sound() # Woof woof!
print(cat.name) # Mimi
cat.sound() # Meow!
When we create Dog("Rex", 2), here’s what happens step-by-step:
- Python calls the
__init__method fromAnimal(becauseDogdoesn’t have its own). - The
nameandagevalues we pass in are stored in that new object. - When we call
dog.sound(), Python looks for asound()method inDogfirst. It finds the one we wrote there and runs it — printing"Woof woof!".
The same logic applies to Cat, but it finds and runs its own sound() method.
This example shows the magic of inheritance:
- We write the common setup (
__init__) only once, in theAnimalclass. - Each subclass (
Dog,Cat) keeps the shared features and adds its own unique behavior. - The result is less repetitive code, more organized logic, and an easier way to manage related objects.
3. How This Works
- Shared structure: The
__init__method fromAnimalruns when you create aDogorCatobject, because they inherit it automatically. - Method overriding: Both
DogandCathave their ownsound()methods that replace (override) the placeholder inAnimal. When you callsound()on aDog, Python uses the one defined inDog, not the one inAnimal.
TIP: If a child class doesn’t override a method, it uses the one from the parent class automatically.
Why Use Inheritance?
Inheritance lets you:
- Avoid repeating code by putting shared logic in a single base class.
- Make your code more modular and readable.
- Build specialized classes from a general blueprint.
Example of thinking in inheritance:
Animal→ general blueprint withname,age, and asound()method.Dog→ a specific type of animal with its own unique sound.Cat→ another specific type of animal with a different sound.
Wrapping Up
Inheritance is a powerful tool in OOP for creating a hierarchy of classes, starting from general concepts and moving toward more specialized ones.
In Python, you implement it by:
- Writing a parent class with common attributes and methods.
- Creating child classes that list the parent class name in parentheses.
- Adding or overriding methods in the child classes as needed.
In the next lesson, we’ll build on this concept by exploring super() — a way for child classes to call and extend the behavior of their parent classes.