DEV Community

Cover image for Python Inheritance
Baransel
Baransel

Posted on

Python Inheritance

Sign up to my newsletter!.

What is Python Inheritance?

When you work in large projects, you will see that the project consists of submodules, subparticles, and many features of these submodules are already in the main module. For this, you will have to create the features and functions for the submodules each time, which will literally turn into a torture for you. This is where the concept of inheritance emerges.

Inheritance, is a concept that comes with object-oriented programming (OOP). If you don't know object-oriented programming logic, you can have a look here. While describing object-oriented programming, we were creating a new class for each object and specifying the properties and functions of each object. Here, with inheritance, which is a much more practical method, you no longer need to specify properties and functions for each object. You use features that you create once, over and over again.

Let's give an example from daily life. For example, you have a vehicle object; You will create objects such as cars, motorcycles. Here you don't need to write properties again for car and engine because the properties of car and motorcycle object already exist in vehicle object. The reason is that car and motorcycle objects are subsets of vehicle objects. If you are confused, don't worry, you will understand much better when we do the examples.

Many of us have student information or parent information systems that we see at university or high school. These systems have more than one user login system. As an example, I took the student information system of a university.

University login page

As you can see, there are more than one different user login to the system and most of the actions of these users are almost the same. Now let's write the codes for these users.

class Academician:
    def __init__(self, name, surname, number):
        self.name = name
        self.surname = surname
        self.number = number

    def login(self):
        print("Signed In")

    def logout(self):
        print("Signed Out")


class Staff:
    def __init__(self, name, surname, number):
        self.name = name
        self.surname = surname
        self.number = number

    def login(self):
        print("Signed In")

    def logout(self):
        print("Signed Out")


class Student:
    def __init__(self, name, surname, number):
        self.name = name
        self.surname = surname
        self.number = number

    def login(self):
        print("Signed In")

    def logout(self):
        print("Signed Out")
Enter fullscreen mode Exit fullscreen mode

If you have noticed, even though we use OOP, we had to write the same codes over and over again. Now let's do it with the concept of inheritance.

Use of Inheritance

Inheritance method is very easy. The general usage is as follows.

class class_name(inherit_class_name):
   ...

Now let's write our codes;

class User:
    def __init__(self, name, surname, number):
        print("User class function")
        self.name = name
        self.surname = surname
        self.number = number

    def login(self):
        print("Signed In")

    def logout(self):
        print("Signed Out")



class Academician(User):
    pass
class Staff(User):
    pass
class Student(User):
    pass
Enter fullscreen mode Exit fullscreen mode

We created our class with User and inherited other objects from this class. We did not add properties and functions for subclasses because we will inherit properties and functions from the User class.

Now let's create and call the object.

academician = Academician("Baransel", "Arslan", 14092000)
staff = Staff("Oscar", "Olson", 16527187)
student = Student("Holger", "Maack", 167281112)

print("Academician")
print(academician.name)
print(academician.surname)
print(academician.number)

print("Staff")
print(staff.name)
print(staff.surname)
print(staff.number)

print("Student")
print(student.name)
print(student.surname)
print(student.number)
Enter fullscreen mode Exit fullscreen mode

Output:

Academician
Baransel
Arslan
14092000

Staff
Oscar
Olson
16527187

Student
Holger
Maack
167281112
Enter fullscreen mode Exit fullscreen mode

As you can see, you can call and use the properties and functions in the user object as you wish. Now, a question has come to our mind as follows; I can use the properties of the object I inherited, but what do I do when I want to add additional properties and functions.

Python Overriding

When we processed the classes, we said that the constructor function __init__() was the reference of the class we created, and this function was running automatically. Therefore, when we want to use the new features, not the features of the function we inherited in our new class, we need to define the __init__() function for the new class. Let's show it now.

class User:
    def __init__(self, name, surname, number):
        print("User class function")
        self.name = name
        self.surname = surname
        self.number = number

    def login(self):
        print("Signed In")

    def logout(self):
        print("Signed Out")



class Academician(User):
    def __init__(self, name, surname, number, birth_date):
        print("Academic class function")
        self.name = name
        self.surname = surname
        self.number = number
        self.birth_date = birth_date

academician = Academician("Baransel", "Arslan", 14092000, 2000)
print(academician.name)
academician.login()
Enter fullscreen mode Exit fullscreen mode

Let's run our code;

Academic class function
Baransel
Signed In
Enter fullscreen mode Exit fullscreen mode

We got an output like We no longer use the __init__() function of the User class, but the __init__() function of the Academician class. But we can also use other functions of the User. Now you will say that we used OOP and inheritance to avoid code duplication and write less code.

We recreated the features we created in the User. Of course there is a solution for this.

super()

This function, ensures that the class it is in is inherited from a higher class, and you can use the features there.

Let's show it now;

Its general usage is as follows;

super().__init__(parent_class_parameters)

Let's do our example right now;

class User:
    def __init__(self, name, surname, number):
        print("User class function")
        self.name = name
        self.surname = surname
        self.number = number

    def login(self):
        print("Signed In")

    def logout(self):
        print("Signed Out")



class Academician(User):
    def __init__(self, name, surname, number, birth_date):
        print("Academic class function")
        super().__init__(name, surname, number)
        self.birth_date = birth_date

academician = Academician("Baransel", "Arslan", 14092000, 2000)
Enter fullscreen mode Exit fullscreen mode

Here, we did not recreate the name, surname and number properties that we created above. We inherit it with the super() function.

Let's run the codes;

Academic class function
User class function
Enter fullscreen mode Exit fullscreen mode

As you can see, both the __init__() function of the User class and the constructor method __init__() of the Academician class worked.

You might also like

Python Functions
Python File Operations
Python Set and Frozenset
Python dictionary and dictionary methods
Python tuple and tuple methods

Top comments (0)