Inheritance
Code sample link: https://replit.com/@jjoco/python-inheritance
Class inheritance is how we can derive a class (called the derived, child, or subclass) from another class (called the base, parent, or superclass). Classes dervied from some base class maintain some, if not many, characteristics of the base class, but does something a bit differently that warrants the creation of a whole new class.
Why use inheritance
Let's say we have a Rectangle class that has fields width and length. Let's say we wanted to create a rectangle whose width and length are the same.
1 | |
Rectangle(some_number , some_number) if there will be a lot of these type of rectangles. To resolve this, we can write a new Square class, which is a rectangle with equivalent width and length.
Inheritance at work
Let's take the simple implementation of the Rectangle class from the previous section:
1 2 3 4 5 6 7 8 9 10 | |
Square using the following syntax:
1 2 3 4 | |
We will need to use the super() function in the derived class's constructor to gain access to the base class's fields and methods:
1 2 3 4 5 6 7 | |
side parameter of the Square as both parameters of the base class's constructor. This allows us to call Rectangle's get_dimensions() and get_area() methods without needing to re-implement them.
We also add a get_side() method to access the side field of the square.
Take the following calling example:
1 2 3 4 | |
get_area() or get_dimensions() we could still call those functions since Square is a Rectangle and Square can access Rectangle's methods. However, we can re-implement a base class method, if we want:
1 2 3 4 5 6 7 | |
get_side() method into get_dimensions(), allowing Square's implementation to take precendence over Rectangle's:
1 2 3 | |