Inheritance & Overriding
Inheritance
In object-oriented programming (OOP), inheritance is a mechanism that allows one class (called the subclass or derived class) to inherit the properties and behaviors of another class (called the superclass or base class). Inheritance is a key concept in OOP that facilitates code reuse and promotes the creation of a hierarchical structure for organizing and modeling software.
Advantages of Inheritance
Inheritance minimizes code repetition -> Since a child class inherits all attributes and methods from the parent, the child class does not need to implement the same attributes and methods
Inheritance promotes organization and hierarchy -> Inheritance cause different classes to be related to each other based on how each class inherits from one and another
Disadvantages of Inhertiance
Inheritance cause classes to be dependent -> If a parent class has inefficiencies and errors, then a child inherits the same inefficiencies and errors
Inheritance causes design issues -> By trying to create classes dependent to each other, you may not be able to create a class that properly satisfies a program requirement because the class must follow the rules of the parents it is attached to.
“When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.”
James Whitcomb Riley It is not always required or even recommended to use inheritance, unless you are 100% sure you are required to do so.
Key Concepts of Inheritance
Base Class (Superclass): The class whose properties and behaviors are inherited by another class.
Derived Class (Subclass): The class that inherits from another class.
Method Overriding: The ability of a subclass to provide a specific implementation for a method that is already defined in its superclass.
Code Reusability: Inheritance promotes the reuse of code by allowing common functionality to be defined in a base class and shared among multiple subclasses.
Hierarchy: Inheritance enables the creation of a hierarchical structure of classes, where more specialized classes inherit from more general ones.
Example Inheritance Code
Vehicle
is the base class with common attributes like make, model, and year, as well as a methoddisplay_info
to display information about the vehicle.Car
is a subclass ofVehicle
. It inherits the attributes fromVehicle
usingsuper().__init__(make, model, year)
, and it also has an additional attributenum_doors
specific to cars. Thedisplay_info
method is overridden to include information about the number of doors.
super()
method
super()
method"Return a proxy object that delegates method calls to a parent or sibling class of type. This is useful for accessing inherited methods that have been overridden in a class."
- Python Documentation
super()
in Python is a built-in function that is used to call a method from a parent class. It is often used in the context of inheritance to invoke a method or constructor from the parent class. This allows you to extend or override functionality in the child class while still reusing code from the parent class.
By using super()
, you make your code more maintainable and allow for changes in the base class without having to update the derived classes manually.
In our example code, super().__init__(make, model, year)
is used to call the constructor (__init__
method) of the parent class (Vehicle
).
This ensures that the make
, model
, and year
attributes are initialized in the Car
instance, leveraging the initialization logic from the Vehicle
class.
Overriding (Polymorphism)
Overriding occurs when two methods with the same method name and parameters within different classes. Overriding is a fundamental concept that shows polymorphism being evident in OOP.
Here are some scenarios when overriding occurs.
There are two classes here:
A parent class called
Vehicle
A child class called
Car
Both Classes have the methods: display_info()
The method has the same parameters
The method behaves differently in each class; therefore, the child class is overriding the behaviour from the parent class.
You can also override built-in magic-methods/built-in base methods from python standard library.
Last updated