Notes
Introduction
Inheritance is one of the most important concepts in object-oriented programming (OOP). It
allows one class to acquire the properties and behaviors of another class. In Python,
inheritance helps programmers reuse existing code instead of rewriting it from scratch.
The concept of inheritance is inspired by real-world relationships. For example, a car and a
motorcycle are both types of vehicles. A Vehicle class can define general features such as
speed and fuel type, while specific classes like Car or Motorcycle inherit those features.
In large software systems, inheritance improves program organization by creating
hierarchical relationships between classes. This makes programs easier to maintain and
expand over time.
In Python development, inheritance is widely used in frameworks, libraries, and application
design because it promotes modular programming and code reuse.
Definition
Inheritance in Python is a mechanism where a new class, called a child class or subclass,
inherits attributes and methods from an existing class called the parent class or superclass.
The child class can use the features of the parent class while also adding new features or
modifying existing ones.
This mechanism allows programmers to create a relationship between classes that reflects
real-world hierarchies.
Inheritance reduces redundancy in programs and allows developers to build complex
systems using simple and reusable components.
Basic Syntax of Inheritance
In Python, inheritance is implemented by defining a new class that includes the parent class
name inside parentheses.
For example, class Car(Vehicle): means that the Car class inherits from the Vehicle class.
The child class automatically gains access to the attributes and methods defined in the
parent class.
, This syntax makes inheritance simple and easy to implement in Python compared to many
other programming languages.
Parent Class
A parent class is the class whose properties and methods are inherited by another class.
It represents a general concept or category from which more specialized classes are
derived.
For example, a class named Animal may contain attributes such as name and age along with
methods such as eat() or sleep().
Other classes such as Dog or Cat can inherit these common features from the Animal class.
Child Class
A child class is the class that inherits features from a parent class.
It can use all accessible methods and attributes of the parent class.
In addition to inherited features, the child class can define its own attributes and methods.
This allows programmers to extend the functionality of existing classes without modifying
the original class.
Types of Inheritance in Python
Python supports multiple forms of inheritance that help represent different relationships
between classes.
Single inheritance occurs when a child class inherits from one parent class.
Multiple inheritance occurs when a child class inherits from more than one parent class.
Multilevel inheritance occurs when a class inherits from another class which itself inherits
from a parent class.
Hierarchical inheritance occurs when multiple child classes inherit from a single parent
class.
Single Inheritance
Single inheritance is the simplest form of inheritance where a class inherits from only one
parent class.
For example, a class Dog may inherit from a class Animal.