Reusability of class with inheritance

Inheritance is one of the main concept in OOP that allows a class to inherit attributes and methods from another class. With inheritance we achieve code reusability and it also helps in creating a hierarchical relationship between classes. Let's understand inheritance with the help of a Person class example. In the real world, each person has attributes such as a name and age, and an occupation. For instance, a Person can be a Teacher, Student, or Employee. If a person is a Student, they would have additional methods like study. If a Person is an Employee, they would have additional properties and methods, such as the ability to work.

In Python, we can model this using inheritance:

  1. Person class: This is the base class that includes common attributes like name and age.

  2. Student class: Inherits from Person and adds methods specific to students, such as study.

  3. Employee class:: Also inherits from Person and adds properties and methods relevant to employees, such as work.

Inheritance syntax

Let’s see how to inherit one class from another in Python.

Get hands-on with 1200+ tech skills courses.