In this article we are going to talk about What is Python Class Inheritance,
Class inheritance is one of the most important concepts in Object Oriented Programming,
if you are familiar with other OOP programming languages like Java or C++, so it has the
same concepts, basically a class inheritance allows us to create a relationship between two
or more classes. now in object oriented programming one class can inherit attributes and
methods from another class.
So now let’s take a look at this image example.

If you take a look at the above image, we have created two classes, the first class is
Class A, in this class we have created two variables and also we have a method. in the
term of Object Oriented Programming, we can call this class as base class, parent class
or super class. we have another class that is Class B, and this class inherited from the
Class A, for this class we can call derived class, child class or sub class. now in class
inheritance when a class extends from another class, the derived class can access to all
attributes and methods of the base class. for example in here our Class B can access to
the fname, lname and full_name() method of our Class A or base class.
Let’s create a practical example in Python Class Inheritance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Person: def __init__(self, name, email): self.name = name self.email = email def full_info(self): print(f'Name Is {self.name} And Email Is {self.email}') class Student(Person): pass stu1 = Student("Parwiz", 'par@gmail.com') stu2 = Student("Geekscoders", 'geekscoders@gmail.com') stu1.full_info() stu2.full_info() |
In the above code we have a base class and we have a derived class, in the base class
we have some instance attributes and a method for printing the variables. now our derived
class can access to all attributes and methods of the base class, for example Student class
can access to the name, email variables and also full_info() method.
You can see that in here we have created the instance for our Student class not
Person class, and in the Student class we don’t have any attributes like name and
email or a method for printing the information, these are added in the Person class.
1 2 3 4 |
stu1 = Student("Parwiz", 'par@gmail.com') stu2 = Student("Geekscoders", 'geekscoders@gmail.com') stu1.full_info() stu2.full_info() |
If you run the code this will be the result.
