Introduction to Inheritance in Python
Inheritance is capability of an class to inherit all methods of base class from which it is derived and can add new features to the class without modifying it. This helps in representing the real world problem better, and also reusability of code with data protection with features like encapsulation in simple language. In Inheritance the class which derives the properties of other class are called as derived class and the class which is derived is known as base class.
The syntax to define a derived class when one or more base classes are to be inherited is as follows:
As shown, the derived-class specifies a comma-separated list of base classes to inherit from in the class definition header.
Let’s start by defining a base class used for all our examples:
self.type = type
returncooked_cuisine = new cuisine('cooked')
The base shown defines an object template used to define cuisines and capture whether it’s a cooked cuisine or not. It also has a constructor used to accept the type of cuisine. Later an object is created of type ‘cooked’.
4.8 (4,376 ratings)
Types of Inheritance in Python
There are mainly two types of inheritance, a combination of which yields every other type.
1. Single Inheritance : One base class, inherited by one derived class. This is the simplest type of inheritance. Also the minimal possible one. The derived class automatically invokes the base class constructor.
2. Multiple Inheritance : Multiple base classes inherited by one derived class. The base class constructors are invoked in the order in which classes were derived.
Derived Types of Inheritance
Combining the above two forms of inheritance can lead to the following types of inheritance:
1. Hierarchical Inheritance: One base class inherited by multiple derived classes. Each derived class will work independently but objects share the class variables across different classes.
2. Multi-level Inheritance: A derived class serving as a base class for another derived class. The base class constructors are invoked recursively in this case.
3. Hybrid Inheritance: A combination of multiple instances of the above-mentioned types of inheritance. This could lead to any imaginable combination of classes.
Examples of Inheritance in Python
Below the are examples of Inheritance in Python:
1. Single Inheritance
Let’s create a class called Indian Cuisine which inherits class cuisine.
self.place = place
returnindian_cuisine = new cuisine('cooked','India')
As shown in a new class, indian_cusine was created which accepts type parameter and invokes a base class constructor, passing the parameter. It additionally creates a new object variable place. This is used only in the derived class and not visible to base class objects.
2. Hierarchical Inheritance
Let’s create a class called ItalianCuisine which inherits class cuisine:
self.place = place
returnitalian_cuisine = new cuisine('cooked','Italy')
As shown in a new class, italian_cusine was created which accepts type parameter and invokes a base class constructor, passing the parameter. It additionally creates a new object variable place. This is used only in the derived class and not visible to base class objects. Now, Since two classes, indian_cusines, and italian_cuisine inherit cuisine class, there’s a hierarchical inheritance implemented.
3. Multiple Inheritance
Let’s create a class called FineDineCuisine which inherits from multiple classes.
self.portion_size = portion_size
returnfine_dine_cuisine = new cuisine('cooked','India',4)
The new class fine_dine_cuisine, inherits from both indian_cuisine and italian_cuisine, inheriting their parameters. It accepts the parameters type, place and portion_size. type and place are passed as arguments for base class constructors. portion_size is a new object parameter not shared to base classes.
Note on Diamond Inheritance:
Now since both indian_cuisine and italian_cuisine inherit from cuisine class, it forms a classic case of diamond inheritance, where multiple instances of a base class are directly/indirectly present for a derived class. In most languages, like c++, this causes a problem, or abstract classes are used. Python, on the other hand, specifies it’s own innovative solution. It inherits the common methods and attributes only once giving preference to classes in the order of inheritance. Thus, here, since cuisine is inherited twice, preference is given to the indian_cuisine version of cuisine, as it’s inherited first.
Note on Class Members:
Any attribute defined within the class definition, but not within a function, becomes a class attribute and is shared across all instances of the class. Thus if one object changes any of these class attributes, the changes are visible to all other instances (whether for the same class object or derived class object). Thus be careful while using class attributes not present within any method definition.
Conclusion – Inheritance in Python
Python has once again defined a very flexible, accommodating and powerful to use when the object-oriented paradigm is the preferred route. It’s definitely a concept everyone can use and should have under there belt. These concepts are used to form the building blocks of any scalable and maintainable piece of software.
With this basic intro to inheritance in python, you can go ahead and target real-world problem statements, and see how well of a design you are able to think of.
This is a guide to Inheritance in Python. Here we discuss the Introduction, Syntax, and types of Inheritance in Python along with different examples and its code implementation. You can also go through our other suggested articles –
- Inheritance in Java
- What is Inheritance in Programming?
- Inheritance in C#
- Overriding in Java
- Inheritance in PHP
- Declaring Abstract Class in C# with Example
- Guide to Single Inheritance in C++
- Tutorial on Single Inheritance in Python
- Guide to Multiple Inheritance in Python
- Hybrid Inheritance in C++ | How to Works?
- Types of Inheritance in C++
- Learn the Hierarchical Inheritance in C++
- Hybrid Inheritance in Java | How to Work?