Inheritance In Python

Praveen Kumar
6 min readNov 27, 2023

In this blog we will learn Inheritance in Python

Why do We need Inheritance in OOPS.,?

In a Object Oriented Programming, we give releases with some features those are latest as of now. After few days or months if we want to implement new features with the existing features. we don’t need to write all the same old code (which as old features) again and with the new code (new features). So to achieve this old + new implementations. we use Inheritance.

What is Inheritance.,?

Inheritance allows us to define a class that inherits all the methods and properties from another class.

Parent class or Base class (Old Class) is the class being inherited from.

Child class or Derived class (New Class)is the class that inherits from another class.

Let us understand this concept with the Real time examples:-

Ex1:- In python software we have Python 2 and Python 3 as well. In those major versions of python we have minor versions as well like python 2.2.3(or) python 3.6.1.

As the developing goes on we will get the major and minor updates for every application or software or product. Let say if we are giving new minor update in python i.e.,3.6.2 which has new features and existing features.

So here New version (Derived class) has new features and Old features (Base class).

Ex2:- We Inherit properties from our parents. In our family, we all have some assets belongs to our parents which means those are belongs to us also right. we are the next generation of our parents. In this you can relate our parents as Base class and our parent assets as Methods and Attributes of the base class.

So the Next generation means us, we have our parents properties and our new properties as well, which means old belongings and new belongings.

So in conclusion, New generation have parents assets and our new assets.

This is Inheritance.

Syntax :-

class A:
pass
class B(A):
pass
class C(B):
pass

In the above syntax Class A is the base class. Class B which extends the class A. As class C which has features of class B and class A.

Benefits of Inheritance:-

  1. It represents real-world relationships
  2. It provides the reusability of a code. We don’t have to write the same code again and again. Also, it allows us to add more features to a class without modifying it.
  3. It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class A.
  4. It gives Less development and maintenance.

Let us Understand more about inheritance by doing a coding exercise.

Now i want to define a class HUMANS. Basically all humans has 2 eyes and 1 nose and 2 hands. Let us do define humans class first.

class Humans:
def __init__(self):
self.num_eyes=2
self.num_ears=2
self.num_heart=1

obj1=Humans()
print(f"Number of Eyes of Humans {obj1.num_eyes}")
print(f"Number of Ears of Humans {obj1.num_ears}")
print(f"Number of Heart of Humans {obj1.num_heart}")

Now i want to Define the new class Men from Humans. Let us do this as well.

class Humans:
def __init__(self):
self.num_eyes=2
self.num_ears=2
self.num_heart=1

class Men(Humans):
print("I Am From Men Class")
obj1=Men()

As you can see Men is a Derived class and Humans is a super class or Base Class.

Now will define a method in super class and men class from humans.

class Humans:
def __init__(self):
self.num_eyes=2
self.num_ears=2
self.num_heart=1
def display_function(self):
print(f"Number of Eyes of Humans {obj1.num_eyes}")
print(f"Number of Ears of Humans {obj1.num_ears}")
print(f"Number of Heart of Humans {obj1.num_heart}")

class Men(Humans):
print("Men has Short Hair")


obj1=Humans()
obj1.display_function()

Now will see how we can access a super class method from the child class.

Let us understand this with a small example

class Humans:
def work(self):
print("I can Work")
def cook(self):
print("I can cook")
class Men(Humans):
print("Men has Short Hair")

obj1=Men()
obj1.work()

Understanding the code:-

In humans class there is a method work but we created a object for men.

As you can see in the above screenshot we can able to access work method from the men class.

Till now we see how we can inherit entire class.

If you want to use same super class method and add some features to that super class method.

Here use super keyword to use the super class method.

class Humans:
def work(self):
print("I can Work from Humans class")
def cook(self):
print("I can cook")
class Men(Humans):
print("Men has Short Hair")
def work(self):
super().work()
print("I can do more work")
print("This method is from Men class")

obj1=Men()
obj1.work()

Execute the above code

Understanding the Code:-

In the above example we created a work method in Humans (super class) and Men(child class).

In men class we used super class properties as well using the like super().work().

You can observe in the output we have Human class work method statements and men class work method statements.

Till now we have seen methods, Let suppose if we have attributes how we can access them will see here.

Observe the code below.

class Humans:
def __init__(self):
self.num_ears=2
self.num_heart=1
def work(self):
print("I can Work from Humans class")

#Men class from Humans
class Men(Humans):
print("Printing from Men class" )
obj1=Men()
print(f"Number of Heart {obj1.num_ears}")

Understanding the code:-

We created a object for men but we can access super class attribute.

Now will do a small exercise, Execute the below code and resolve the error.

class Humans:
def __init__(self):
self.num_ears=2
self.num_heart=1
def work(self):
print("I can Work from Humans class")

#Men class from Humans
class Men(Humans):
def __init__(self,name):
self.name=name
print("Printing from Men class" )
obj1=Men("Praveen")
print(f"Number of Heart {obj1.num_ears}")

Error Screenshot.

Reason:- In the Men class we have our own init method and we are passing the name as a attribute.

At the time of creating the object we are created using Men class in Line 14 we are printing the attribute of human class.

To Access the attributes of your super class we should use super for init method (super().__init__).

Observe the below code

class Humans:
def __init__(self):
self.num_ears=2
self.num_heart=1
def work(self):
print("I can Work from Humans class")

#Men class from Humans
class Men(Humans):
def __init__(self,name):
super().__init__()
self.name=name
print("Printing from Men class" )
obj1=Men("Praveen")
print(f"Number of Heart {obj1.num_ears}")

Now will see how inheritance behave when passing the arguments.

Look into the code below.

class Humans:
def __init__(self,name):
self.num_ears=2
self.name=name
self.num_heart=1
def work(self):
print("I can Work from Humans class")

#Men class from Humans
class Men(Humans):
def __init__(self,number,name):
super().__init__(name)
self.number=number
self.name=name
def display(self):
print(f"Number of ears of Men {self.num_ears}")

obj1=Men(4,"Praveen")
obj1.display()

Understanding the Code:-

As you can observe we are passing name argument to Humans class and Number argument to Men class. If you want to access the super class attributes inside the child class, you should specify the __init__ of super class with a super keyword with exact arguments. looks like (super.__init__(arguments)).

Remember:- Inheritance gives us the reusability of the code.

Hope you Like this blog. See you in the next blog.

DO FOLLOW ME ON INSTAGRAM :- learn_with_praveen

— — — — — — — — — — — — — — — Praveen Writings

--

--