VOOZH about

URL: https://www.geeksforgeeks.org/python/inheritance-in-python/

⇱ Inheritance in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Inheritance in Python

Last Updated : 5 Jun, 2026

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (called a child or derived class) to inherit attributes and methods from another class (called a parent or base class).

Here, a parent class Animal is created that has a method info(). Then a child class Dog is created that inherits from Animal and adds additional behavior.


Output
Animal name: Buddy
Buddy barks

Explanation:

  • class Animal defines the parent class.
  • info() prints the name of the animal.
  • class Dog(Animal) defines Dog as a child of Animal class.
  • d.info() calls parent method info() and d.sound() calls child method.
👁 animal_class
Inheritance in Python

Benefits Of Inheritance

  • Promotes code reusability by sharing attributes and methods across classes.
  • Models real-world hierarchies like Animal -> Dog or Person -> Employee.
  • Simplifies maintenance through centralized updates in parent classes.
  • Enables method overriding for customized subclass behavior.
  • Supports scalable, extensible design using polymorphism.

super() Function

super() function is used to call methods from a superclass following Python’s Method Resolution Order (MRO). In particular, it is commonly used in the child class's __init__() method to initialize inherited attributes. This way, the child class can leverage the functionality of the parent class.

Example: Here, Dog uses super() to call Animal's constructor


Output
Animal name: Buddy
Buddy is a Golden Retriever

Explanation:

  • super() function is used inside __init__() method of Dog to call the constructor of Animal and initialize inherited attribute (name).
  • This ensures that parent class functionality is reused without needing to rewrite the code in the child class.

Method Overriding in Inheritance

Method overriding allows a child class to provide its own implementation of a method that already exists in the parent class. This enables customized behavior while still maintaining the inheritance relationship.


Output
Bark

Explanation:

  • Animal defines a method sound().
  • Dog inherits from Animal and overrides the sound() method.
  • When d.sound() is called, Python executes the overridden method in the child class instead of the parent class method.

Related Articles:

Comment
Article Tags:
Article Tags: