Subclass in Python

A subclass in Python is a fundamental concept in object-oriented programming that allows a new class to inherit attributes and methods from an existing class, known as the superclass. This mechanism enables code reuse and specialization, allowing developers to create a hierarchy of classes that share common behavior while also implementing specific functionalities unique to each subclass.

Overview

A subclass is designed to extend or specialize the behavior of its superclass. This hierarchical structure reflects the relationships and shared behaviors among classes, promoting a clear and logical organization of code. By inheriting from a superclass, a subclass can utilize its existing functionality and introduce new behaviors or modify existing ones to meet specific needs.

Creating a Subclass

To create a subclass in Python, you define a new class and specify the superclass in parentheses following the class’s name. For example:

class Employee:
   def __init__(self, name, employee_id):
       self.name = name
       self.employee_id = employee_id

   def login(self):
       print(f"An employee {self.name} just logged in.")

   def logout(self):
       print(f"An employee {self.name} just logged out.")


class Supervisor(Employee):
   pass

In this example, Supervisor is a subclass of Employee. The Supervisor class inherits all non-private attributes and methods from the Employee class, allowing it to utilize these functionalities without redefining them.

Inheriting Superclass’s Attributes and Methods

When a subclass is defined, it automatically inherits all non-private attributes and methods from its superclass. This means that the subclass can use these inherited attributes and methods directly. For instance, the Supervisor class can use the login and logout methods from the Employee class without any additional code.

Overriding Superclass’s Methods

Complete Overriding

Complete overriding occurs when a subclass provides a new implementation for a method that exists in its superclass, replacing the superclass’s implementation entirely. For example, if the Supervisor class were to redefine the login method, it would completely override the login method from the Employee class.

Partial Overriding

Partial overriding allows a subclass to extend the functionality of a superclass’s method by calling the superclass’s method using super() and then adding additional behavior. For example, the logout method in Supervisor could call super().logout() and then print an additional message, thereby extending the functionality of the logout method from the Employee class.

Identifying Use Scenarios for Subclasses

Subclasses are particularly useful when you have multiple classes with similar functionalities, which can lead to code repetition. By creating a superclass to handle shared functionalities, you can reduce code duplication and implement specific functionalities in subclasses. This approach not only streamlines the codebase but also makes it easier to maintain and extend.

Using Behavior of Superclasses

Subclasses are designed to use or specialize the behavior of their superclasses, maintaining the ‘is-a’ relationship, a fundamental concept in object-oriented programming. This relationship ensures that a subclass is a specific instance of its superclass, meaning it can be used interchangeably with its superclass without altering the correctness of the program. This property is known as substitutability.

Visual Representation

Figure 8.9

Figure 8.9 Creating a superclass that handles the shared attributes and methods. In the subclasses, you implement specific attributes and methods. You should also note that by default, the subclasses inherit all nonprivate attributes and methods from the superclass.

This figure illustrates the concept of a superclass handling shared attributes and methods, while subclasses implement specific attributes and methods. It highlights the default behavior where subclasses inherit all non-private attributes and methods from the superclass.

For more detailed information, you can refer to the Python How-To and Practices of the Python Pro books.

Book TitleUsage of subclassTechnical DepthConnections to Other ConceptsExamples UsedPractical Application
Python How-ToDiscusses subclass as a mechanism for code reuse and specialization, allowing for a hierarchy of classes. moreCovers creating subclasses, inheriting attributes and methods, and overriding methods. moreConnects subclassing to code reuse, specialization, and hierarchy. moreProvides code snippets for creating subclasses and overriding methods. moreIdentifies scenarios where subclasses reduce code duplication and streamline codebase. more
Practices of the Python ProDefines subclass as a way to specialize or extend superclass behavior, emphasizing the ‘is-a’ relationship. moreFocuses on the ‘is-a’ relationship and substitutability in object-oriented programming. moreHighlights the importance of the ‘is-a’ relationship and substitutability. moreDiscusses the use of subclassing to maintain a logical class hierarchy. moreEmphasizes subclassing for maintainable and scalable software designs. more

FAQ (Frequently asked questions)

What do subclasses automatically inherit from their superclass?

Why is the ‘is-a’ relationship important in inheritance?

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest