Subclassing and Derivation

Subclassing, also known as derivation, lets you create a new class from an existing one in object-oriented programming (OOP). Through this technique, the subclass, child class, or derived class inherits features from the parent class, base class, or superclass, forming a hierarchical relationship between classes. Object oriented programming often interchanges parent/superclass/base and child/subclass/derived.
Inherited relationships are “is a” relationships in which one class is a “type of” another. A daffodil, for instance, is a type of plant, and a mechanic is a sort of person. Because of this relationship, modularity is encouraged and redundancy is decreased because the subclass can inherit properties and methods from its superclass without having to redefine them.
Purpose and Benefits of Subclassing
Enabling code reuse and customisation is the main objective of subclassification. Subclasses are used to improve the data structures and behaviour that a superclass has left behind. A parent class can specify shared or generic properties and methods that apply to a collection of related objects. These common characteristics can then be inherited by subclasses, which can subsequently add distinctive characteristics and behaviours of their own.
Instead than changing existing code or beginning from scratch, this method enables developers to program by customising what already exists. Subclassing offers an organised method of adding new functionality to an existing class without changing its original specification. Factoring code is supported to reduce redundancy and make maintenance easier. When inheritance is used well, it can cut down on development time considerably.
How Subclassing Works in Python
In Python, a subclass is defined by enclosing the superclass name (or superclasses, if multiple inheritance is used) in parenthesis within the class specification header:
class SuperClass:
# Attributes and methods for the superclass
pass
class SubClass(SuperClass): # SubClass inherits from SuperClass
# Attributes and methods for the subclass
pass
A subclass instance immediately inherits all of the methods and properties specified in its superclass when it is created. The same syntax used to access the members of the subclass itself can also be used to access these inherited members.
Python looks for attributes (data or methods) using an attribute inheritance search mechanism. Python looks first in the object (the instance) itself for the attribute when you call object.attribute. It looks through the class from which the instance was created if it cannot be located. Searching all higher superclasses from bottom to top (and left to right in the case of multiple inheritance) if nothing is found, it moves up the hierarchy. The attribute’s discovery marks the end of the search.
Modifying Inherited Behavior
In addition to utilising inherited characteristics, subclasses have the ability to alter or expand them.
Overriding Methods: A method with the same name in the subclass can be defined to override a superclass method. When the method is invoked on a subclass instance, the version of the subclass is run rather than the version of the superclass. This is because, because the subclass is lower in the hierarchy, the inheritance search locates the method name first.
Using the references given, the following example illustrates method overriding
Extending Methods: Occasionally, you wish to enhance an inherited method rather than replace it entirely. Redefining the method in the subclass while invoking the superclass version from within the new method is one way to accomplish this. In addition to the superclass’s usual behaviour, this enables the subclass to carry out its own unique activities.
The superclass method can be used with its name followed by (self) or, more frequently in contemporary Python, with the super() function
Inheriting from and adding to Constructor (init): In order to properly configure inherited attributes, a subclass frequently needs to utilise both its own init method and the init method of the superclass. The superclass is invoked when an instance is created if the subclass does not specify init. Usually, if the parent defines init, super().init calls it.
Adding New Attributes and Methods
In addition to changing inherited characteristics, a subclass can add completely new methods and attributes that aren’t in the superclass. These new members are not available on instances of the parent class; instead, they are unique to the subclass and its instances. For instance, an ordinary Employee class might not contain the programming language attribute and update codebase method that a Developer subclass might.
Alternatives and Variations
Even though inheritance is a great way to represent “is a” connections and encourage reuse through customisation, it can be misused, which makes managing applications challenging. Instead, “has a” relationships where one object contains instances of other things to construct larger structures are frequently highlighted using alternative strategies, such as composition.
Python additionally facilitates multiple inheritance, allowing a class to inherit from many superclasses, which are listed in the class header’s parentheses. For the attribute inheritance search (Method Resolution Order, or MRO), the listing order of superclasses is crucial.
In conclusion, Python derivation and subclassing are essential OOP methods for creating new classes from preexisting ones. They model “is a” relationships between classes, make it easier to reuse code, permit the addition of new, specialised features, and allow customisation of inherited behaviour through overriding or extending methods.