Extend a Class in Python

Extend a Class in Python

In Python, we can extend a class to create a new class from the existing one. This becomes possible because Python supports the feature of inheritance.

Using inheritance, we can make a child class with all the parent class’s features and methods. We can also add new features to the child class other than those present in the parent class.

We can even override those features of the parent class that we don’t need. You will learn how to do all that as you go through this article.

Extend a Class in Python

In simple terms, extending a class means that we want to create a new class or child class from an existing or parent class. Extending a class is the same as inheriting a class.

Let us see how inheritance works in Python.

Extend a Class Using Inheritance in Python

To inherit a class in Python, we pass the name of that class as a parameter while creating the child class.

Syntax:

class ChildClass(ParentClass)

Let us understand this with an example.

We first create a parent class, Desserts, with two methods - init and intro. The method intro has a print statement that prints the flavor and color shown in the output.

class Desserts:
    def __init__(self, flavor, color):
        self.flavor = flavor
        self.color = color

    def intro(self):
        print(self.flavor, self.color)

obj = Desserts("Vanilla", "Pink")
obj.intro()

Output:

Vanilla Pink

We create the child class, Cake that will inherit the class, Desserts. For the Cake class to inherit the Desserts class, we will pass the name of the Desserts class as a parameter while creating the Cake class.

class Cake(Desserts):
    pass

We are using the pass keyword here because we are only inheriting the class, Desserts, and not adding any other method or feature to the child class, Cake. The cake class now has the same attributes and methods as the Desserts class.

We can verify this by creating an object of the Cake class and then executing the intro method as shown in the last two lines of code.

class Desserts:
    def __init__(self, flavor, color):
        self.flavor = flavor
        self.color = color

    def intro(self):
        print(self.flavor, self.color)

obj = Desserts("Vanilla", "Pink")
obj.intro()

class Cake(Desserts):
    pass

obj = Cake("Black forest", "Black")
obj.intro()

Output:

Vanilla Pink
Black forest Black

See how the Cake class also executes the intro method like the Desserts class.

Let us see how we can add more methods and attributes to this newly created child class.

Add the __init__() Function to the Child Class After Extending a Class

We can add a new init() function to the child class even when inheriting a class. Note that whenever an object of a class is created, the init() function is automatically called.

Also, adding the init() function to the child class will not use the parent class’s init() function.

class Cake(Desserts):
    def __init__(self, flavor, color):
        self.flavor = flavor
        self.color = color

Although the init() method of the child class overrides the inheritance of the init() method of the parent class, we can still use the parent’s init() method by calling it like this:

class Cake(Desserts):
    def __init__(self, flavor, color):
        Desserts.__init__(self, flavor, color)

Use the super() Function After Extending a Class in Python

In Python, the super() function can be used to access the attributes and methods of a parent class.

When there is a new init() inside a child class that is using the parent’s init() method, then we can use the super() function to inherit all the methods and the properties from the parent class.

class Cake(Desserts):
    def __init__(self, flavor, color):
        super().__init__(flavor, color)

Note that here, we are not specifying the name of the parent class; the super() function automatically identifies it.

This was all about the basics of inheritance in Python.

But what if we have to add more attributes or methods to the child class? Let us see how we can do that.

Add Attributes to the Child Class After Extending a Class in Python

We can add extra attributes to a child class other than those inherited by the parent class just like we add any other attribute. See how a property called quantity is added to the Cake class.

class Cake(Desserts):
    def __init__(self, flavor, color, quantity):
        super().__init__(flavor, color)
        self.quantity = quantity

We added one more parameter in the child class’s init() function. Also, do not forget to pass one more value for quantity while creating the object of the Cake class, like this:

obj = Cake("Black forest", "Black", 5)

Add Methods to the Child Class After Extending a Class in Python

We add a method price in the child class by simply using the def keyword in the code below. We also pass the self keyword as the parameter.

class Cake(Desserts):
    def __init__(self, flavor, color, quantity):
        super().__init__(flavor, color)
        self.quantity = quantity

    def price(self):
        print("Pay for: ", self.quantity, "items.")

The entire code would be as follows:

class Desserts:
    def __init__(self, flavor, color):
        self.flavor = flavor
        self.color = color

    def intro(self):
        print(self.flavor, self.color)

obj = Desserts("Vanilla", "Pink")
obj.intro()

class Cake(Desserts):
    def __init__(self, flavor, color, quantity):
        super().__init__(flavor, color)
        self.quantity = quantity

    def price(self):
        print("Pay for: ", self.quantity, "items")

obj = Cake("Black forest", "Black", 5)
obj.intro()
obj.price()

Output:

Vanilla Pink
Black forest Black
Pay for:  5 items

Note that adding a method with the same name as any method in the parent class will override the inherited method.

This is how we can extend a class in Python. Refer to this official documentation to learn more.

Related Article - Python Class

  • Python Generator Class
  • Data Class Inheritance in Python
  • Serialize a Python Class Object to JSON
  • Python Abstract Property
  • Python Class Factory
  • Python Class Equality