Open In App

Extend Class Method in Python

Last Updated : 10 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, class methods are functions that are bound to the class rather than the instance of the class. This means they receive the class (cls) as the first argument instead of the instance (self). Extending a class method refers to enhancing or customizing the behavior of an inherited class method in a subclass. Example:

Python
class Vehicle:
    @classmethod
    def start(cls):
        return "Vehicle starting"

class Car(Vehicle):
    @classmethod
    def start(cls):
        return f"{super().start()} → Car starting"

print(Car.start())

Output
Vehicle starting → Car starting

Explanation:

  • Vehicle Class defines a class method start() that returns "Vehicle starting".
  • Car Class inherits from Vehicle and overrides the start() method.
  • super().start() calls the start() method from Vehicle, returning "Vehicle starting" and appends " → Car starting".

Why extend a class method?

Extending a class method allows you to:

  • Reuse existing logic from the parent class.
  • Add or modify behavior specifically for the subclass.
  • Keep your code DRY (Don't Repeat Yourself) and more maintainable.
  • Follow OOP principles, especially inheritance and polymorphism.

By using super(), you can call the method from the parent class and build upon it.

Syntax

class Parent:

@classmethod

def method(cls):

# base class method logic

class Child(Parent):

@classmethod

def method(cls):

super().method() # extend base class method

# child class method logic

Examples of extending class methods

Example 1: In this example, we are extending and modifying the behavior of a parent class method in the subclass, while preserving the original behavior by using super().

Python
class Animal:
    @classmethod
    def sound(cls):
        print("Animal sound")

class Dog(Animal):
    @classmethod
    def sound(cls):
        super().sound()
        print("Dog bark")

Dog.sound()

Output
Animal sound
Dog bark

Explanation:

  • Animal Class defines a class method sound() that prints "Animal sound".
  • Dog Class inherits from Animal and overrides the sound() method.
  • super().sound() in the Dog class, it calls the sound() method from Animal using super(), printing "Animal sound".

Example 2: This example defines a Vehicle class with a method that returns the string "Vehicle", a Car class that extends Vehicle and modifies the start method and an ElectricCar class that further extends Car and overrides the start method.

Python
class Vehicle:
    @classmethod
    def start(cls):
        return "Vehicle"

class Car(Vehicle):
    @classmethod
    def start(cls):
        return f"{super().start()} → Car"

class ElectricCar(Car):
    @classmethod
    def start(cls):
        print(f"{super().start()} → ElectricCar")

ElectricCar.start()

Output
Vehicle → Car → ElectricCar

Explanation:

  • Vehicle Class defines a class method start() that returns "Vehicle".
  • Car Class inherits from Vehicle and overrides start() to call super().start() (from Vehicle) and adds " → Car".
  • ElectricCar Class inherits from Car and overrides start() to call super().start() (from Car) and prints " → ElectricCar".

Example 3: This code defines a Triangle class that tracks the number of instances created, and a Perimeter class that calculates and displays the perimeter of the triangle.

Python
class Triangle:
    count = 0
    def __init__(self, name, a, b, c):
        self.name, self.a, self.b, self.c = name, a, b, c
        Triangle.count += 1

class Perimeter(Triangle):
    def display(self):
        return f"{self.name}: {self.a}, {self.b}, {self.c} | Perimeter: {self.a + self.b + self.c}"

print(Perimeter("PQR", 2, 3, 4).display())

Output
PQR: 2, 3, 4 | Perimeter: 9

Explanation:

  • Triangle Class initializes a triangle with a name, sides a, b, c and increments the count of instances.
  • Perimeter Class inherits from Triangle and defines display() to show the triangle’s name, sides and perimeter.

Next Article
Practice Tags :

Similar Reads