Inheritance in Python
Inheritance is one of the most important aspects of Object Oriented Programming. While programming, many a times, situations arise where we have to write a few classes with some common features and some unique, class-specific features, which include both variables and methods.
In such situations, as per object oriented programming, we can take out the common part and put it in a separate class, and make all the other classes inherit this class, to use its methods and variables, hence reducing re-writing the common features in every class, again and again.
The class which inherits another class is generally known as the Child class, while the class which is inherited by other classes is called as the Parent class.
Ofcourse, you must only use this for the related classes only, for example, you can define a class LivingOrganism with all the basic features of a living organism defined in it, like breathe, eat etc. Now this class can easily be re-used by another class Animals and HumanBeings, as both of these shares the features.
Also, at times, Inheritance is used to simplify big classes with a lot of variables and methods, into smaller classes by breaking down the functionality into core features and secondary features. The core features are generally kept in the parent class.
Syntax for Inheritance
If we have a class Parent
and another class Child
and we want the class Child
to inherit the class Parent
, then
# Parent class
class Parent:
# class variable
a = 10;
b = 100;
# some class methods
def doThis();
def doThat();
# Child class inheriting Parent class
class Child(Parent):
# child class variable
x = 1000;
y = -1;
# some child class method
def doWhat();
def doNotDoThat();
By specifying another class's name in parentheses, while declaring a class, we can specify inheritance. In the example above, all the properties of Parent
will be inherited to the Child
class. With this, all the methods and variables defined in the class Parent
becomes part of Child
class too.
Time for an Example
Let's take simple example. Animals can be divided into multiple types like reptiles, mammals, amphibians etc. While they all are different physically and biologically, there are many characteristics that are common amongst them. So now, we will define a parent class with name Animal, which will have some basic properties and functions related to animals.
And then we will define classes for various other types, and all those classes will also inherit the class Animal.
Here we have the Animal
class.
class Animal:
# properties
multicellular = True
# Eukaryotic means Cells with Nucleus
eukaryotic = True
# functions
def breath();
def feed();
Now let's define a class for Mammals. As mammals are animals with warm blood, who produce milk for their infants etc, hence our Mammal
class will look like,
class Mammal(Animal):
# properties
haveMammaryGland = True;
def warmBlood = True;
# functions
def produceMilk();
In case we want to create another class for Amphibians
too, then
class Amphibian(Animal):
# properties
liveInWater = True;
# functions
def metamorphosis();
As the classes Mammals
and Amphibian
both inherit the class Animal
, hence they will have the properties and functions defined in the class Animal
.
Hence, now any object of Amphibian
class, say Frog, will have the properties: multicellular
(from class Animal), eukaryotic
(from class Animal), liveInWater
(from class Amphibian), and would be able to breath()
, feed()
and do metamorphosis()
.
And how all this can be written as code, if we create an object, say Frog:
>>> Amphibian Frog = Amphibian()
>>> Frog.breath(); # calling function defined in Animal class
>>> Frog.metamorphosis(); # calling function defined in Amphibian class
>>> print (Frog.liveInWater)
True
Benefits of using Inheritance
Here are a few main advantages of using Inheritance in your programs.
- Less code repeatition, as the code which is common can be placed in the parent class, hence making it available to all the child classes.
- Structured Code: By dividing the code into classes, we can structure our software better by dividing functionality into classes.
- Make the code more scalable.
Accessing Parent Class Element in Child Class
While working in a child class, at some point you may have to use parent class's properties or functions. In order to access parent class's elements you can use the dot .
operator.
Parent.variableName
Mentioned above is how you can access the variable, or in case you need to call parent class's function then,
Parent.functionName()
Where Parent
is the name of our parent class, and variableName
and functionName()
are its variable and function respectively.
Below is an example, we have a simple example to demonstrate this:
class Parent:
var1 = 1
def func1(self):
# do something here
class Child(Parent):
var2 = 2
def func2(self):
# do something here too
# time to use var1 from 'Parent'
myVar = Parent.var1 + 10
return myVar