Python Inheritance

← Back to Home

Inheritance in Python (learn Simple)



Inheritance is one of the most important concepts in OOP (Object-Oriented Programming). It allows one class to use the properties and methods of another class.

Inheritance allows us to define a class that can inherit all the methods and properties from another class.

Think of it like a family: a child can inherit features from their parents.

Parent class is the class being inherited from, also called base class.

Child class is the class that inherits from another class, also called derived class.

  • In programming, a class (child) can inherit features (methods and variables) from another class (parent).

For more on Python classes, check out our post on Python Classes and Objects .



✅ Why to Use Inheritance?

  • To reuse code.
  • To make the program easier to manage.
  • To follow the DRY principle (Don't Repeat Yourself).


Basic Syntax

class Parent:
    # parent class
    pass

class Child(Parent):
    # child class that inherits from Parent
    pass


Example: Simple Inheritance

# Parent class
class Animal:
    def speak(self):
        print("I am an animal")

# Child class
class Dog(Animal):
    def bark(self):
        print("Woof!")

# Creating object of Dog
my_dog = Dog()

my_dog.speak()  # Inherited from Animal
my_dog.bark()   # Defined in Dog

Output:

I am an animal
Woof!

Here, the Dog class inherits the speak() method from the Animal class. It also has its own method called bark().



Example 2: Inheriting from a Person Class

Let’s create a simple example where a Student class inherits from a Person class.

Step 1: Create the Parent Class

# Parent class
class Person:
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname

    def printname(self):
        print(self.firstname, self.lastname)

# Creating an object of Person
x = Person("Steve", "Tendulkar")
x.printname()

Output:

Steve Tendulkar


Step 2: Create the Child Class

# Child class that inherits from Person
class Student(Person):
    pass  # No additional code, just inheriting everything from Person

# Creating an object of Student
x = Student("Mike", "Olsen")
x.printname()

Output:

Mike Olsen


📝 Note:
The pass keyword is used when you don not want to add any extra functionality to the child class. The Student class automatically gets all the properties and methods from the Person class.



Types of Inheritance in Python

  1. Single Inheritance – One child inherits from one parent.

  2. Multiple Inheritance – One child inherits from multiple parents.

  3. Multilevel Inheritance – A child inherits from a parent, and another child inherits from that child.

  4. Hierarchical Inheritance – Multiple children inherit from one parent.

  5. Hybrid Inheritance – A mix of any of the above types.




Example: Multiple Inheritance in Python

class Father:
    def skill(self):
        print("Good at driving")

class Mother:
    def talent(self):
        print("Good at cooking")

class Child(Father, Mother):
    def hobby(self):
        print("Loves painting")

c = Child()
c.skill()
c.talent()
c.hobby()

Output:

Good at driving
Good at cooking
Loves painting


💡 Important Points to note

  • The super() function is used to call methods from the parent class.
  • Inheritance makes your code organized and reusable.
  • If both parent and child have the same method, the child’s method will override the parent’s method.


Using super() Example

class Animal:
    def speak(self):
        print("Animal speaks")

class Dog(Animal):
    def speak(self):
        super().speak()
        print("Dog barks")

d = Dog()
d.speak()

Output:

Animal speaks
Dog barks


Conclusion

Inheritance helps us write cleaner and much more efficient code. Once we understand how it works, we can build complex applications more easily by reusing and extending existing code. It saves times and provides better code management.

For more on Python classes, check out our post on Python Classes and Objects.