From Basics to Mastery: Understanding Inheritance in Python

Before we look at how to accomplish inheritance in Python, let’s first describe it.

What is Inheritance?

A key idea in object-oriented programming languages (OOP) is inheritance, which allows a class to inherit characteristics and actions (methods) from another class. This makes it possible for classes to have a hierarchical relationship in which subclasses can reuse code from their superclass, encouraging code reuse and cutting down on redundancy.

When a subclass inherits from a superclass, it automatically gains access to all public and protected members of the superclass. These members include variables, methods, and other properties defined in the superclass. The subclass can then extend or override these members, meaning it can provide its own implementation of inherited methods or define additional methods and attributes.

Inheritance facilitates the creation of more specialised classes that represent more specific types of objects, while still retaining the common characteristics and behaviours defined in the superclass. This promotes modularity, maintainability, and extensibility of code.

We can achieve inheritance in Python using pass keyword or using __init__ keyword.

Let’s dive into it…

First we need to Create a Parent Class(Any class can be a parent class) with its properties and methods.

class Person:
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname
    def printname(self):
        print(self.firstname, self.lastname)

Using pass keyword

Create a new class and pass the parent class as a parameter to inherit from the parent class.

class Student(Person):
  pass

If you realized we Used the pass keyword here by using pass keyword you can not add any other properties or methods to the class.

Using __init__ keyword

By Passing the Parent class as a parameter into the other class it will be treated as a child class and this way  you can achieve the inheritance in python.

class Student(Person):
    def __init__(self, fname, lname, year):

The __init__() function is called automatically every time the class is being used to create a new object.

When you add the __init__() function, the child class will no longer inherit the parent’s __init__() function as it overrides the inheritance of the parent’s __init__() function.

To keep the inheritance of the parent’s __init__() function, add a call to the parent’s __init__() function

class Student(Person):
  def __init__(self, fname, lname):
    Person.__init__(self, fname, lname)

Now we have successfully added the __init__() function, and kept the inheritance of the parent class, and we are ready to add functionality in the __init__() function.

Use the super() Function.

Python also has a super() function that will make the child class inherit all the methods and properties from its parent.

class Student(Person):
  def __init__(self, fname, lname):
    super().__init__(fname, lname)

By using the super() function, you do not have to use the name of the parent element, it will automatically inherit the methods and properties from its parent.

Adding Methods and Properties

You can add new methods and properties to the child class.

class Student(Person):
    def __init__(self, fname, lname, year):
        super().__init__(fname, lname)
        self.graduationyear = year
    def welcome(self):
        print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)

Here’s a link to some books that you can read to gain more knowledge. Happy Learning!!

By following these steps, you can achieve inheritance in Python. As I said earlier Inheritance allows you to create a hierarchy of classes that share a set of properties and methods from the parent class. This approach helps in reusing code and representing real-world relationships effectively.

Leave a Reply