← Back to Python Course | Chapter 7: OOP | Lesson 4 of 11

Python Inheritance

Inheritance lets a new class receive everything from an older one, like a child inheriting eye color from a parent. You can then add or change just what is different.
Syntax
python
class ParentClass:
    # parent members

class ChildClass(ParentClass):
    # child members
    # super().method_name() calls the parent

What is Inheritance?

Inheritance lets a child class automatically pick up the attributes and methods of a parent class, so common behavior (like a general Animal.eat() method) only needs to be written once and every subclass (Dog, Cat) gets it for free.

Example: What is Inheritance?

python
class Animal:
    def eat(self):
        print("Eating")

class Dog(Animal):  # Dog inherits eat() from Animal
    pass

Dog().eat()

Creating a Subclass

Writing class Dog(Animal): makes Dog a subclass of Animal -- every method and attribute Animal defines becomes available on Dog instances immediately, even before you add anything Dog-specific to its own body.

Example: Creating a Subclass

python
class Animal:
    def eat(self):
        print("Eating")

class Dog(Animal):  # subclass of Animal
    def bark(self):
        print("Woof")

d = Dog()
d.eat()  # inherited from Animal
d.bark()  # defined directly on Dog

Method Overriding

If a child class defines a method with the same name as one in its parent, calling that method on a child instance runs the child's version instead -- this is how subclasses customize inherited behavior without touching the parent class at all.

Example: Method Overriding

python
class Animal:
    def speak(self):
        print("Some sound")

class Dog(Animal):
    def speak(self):  # overrides Animal's speak method
        print("Woof")

Dog().speak()  # runs Dog's version, not Animal's

The super() Function

super() gives a child class a handle back to its parent, most often used inside __init__ as super().__init__(...) to run the parent's setup logic before adding the child's own attributes, so you don't have to duplicate the parent's initialization code.

Example: The super() Function

python
class Animal:
    def __init__(self, name):
        self.name = name

class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name)  # runs Animal's __init__ to set self.name
        self.breed = breed

d = Dog("Rex", "Labrador")
print(d.name, d.breed)

Multiple Inheritance

A single class can list more than one parent -- class Duck(Swimmer, Flyer): -- inheriting from both, which is powerful for combining independent capabilities but requires understanding method resolution order once those parents define overlapping method names.

Example: Multiple Inheritance

python
class Swimmer:
    def swim(self):
        print("Swimming")

class Flyer:
    def fly(self):
        print("Flying")

class Duck(Swimmer, Flyer):  # inherits from both parent classes
    pass

d = Duck()
d.swim()
d.fly()
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting to call super().__init__() in a subclass constructor, so the parent's attributes are never set and later access raises AttributeError.
  2. Overriding a method with a different number of parameters, so code that expects the parent's signature breaks.
  3. Using inheritance for an "has-a" relationship, such as making Car inherit from Engine, instead of storing an engine as an attribute.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.