Pythondex

Tutorial

Inheritance in Python

In Python, inheritance lets a class use code from another class. The class that gives code is called parent, and the class that gets it is called child.

Basic Inheritance

Here, Car inherits methods from Vehicle:

Overriding Methods

A child class can have its own version of a method. This is called overriding.

Using super()

You can call the parent’s method inside the child using super().

Child Class with Extra Attributes

Child classes can also have extra attributes or methods not in the parent.

Multiple Child Classes

Multiple child classes can inherit from the same parent class.

What You Learned

  • Inheritance lets a child class use code from a parent class.
  • Child classes can add their own methods or attributes.
  • Child classes can override parent methods.
  • You can use super() to call parent methods inside a child.
  • Multiple child classes can inherit from the same parent.