Monday, March 21, 2022

Extending classes with inheritance

The concept of inheritance in OOP is similar to the concept of inheritance in the real world, where children inherit some of the characteristics from their parents on top of their own characteristics.

Similarly, a class can inherit elements from another class. These elements include attributes and methods. The class from which we inherit another class is commonly known as a parent class, a superclass, or a base class. The class we inherit from another class is called a derived class, a child class, or a subclass. The following screenshot shows a simple relationship between a parent class and a child class:


In Python, when a class inherits from another class, it typically inherits all the elements that compose the parent class, but this can be controlled by using naming conventions (such as double underscore) and access modifiers.

Inheritance can be of two types: simple or multiple. Let us discuss these.

Simple inheritance

In simple or basic inheritance, a class is derived from a single parent. This is a commonly used inheritance form in OOP and is closer to the family tree of human beings. The syntax of a parent class and a child class using simple inheritance is shown next:

class BaseClass:

<attributes and methods of the base class >

class ChildClass (BaseClass):

<attributes and methods of the child class >

For this simple inheritance, we will modify our example of the Car class so that it is derived from a Vehicle parent class. We will also add a Truck child class to elaborate on the concept of inheritance. Here is the code with modifications:

#inheritance1.py

class Vehicle:

def __init__(self, color):

self.i_color = color

def print_vehicle_info(self):

print(f"This is vehicle and I know my color is \

{self.i_color}")

class Car (Vehicle):

def __init__(self, color, seats):

self.i_color = color

self.i_seats = seats

def print_me(self):

print( f"Car with color {self.i_color} and no of \

seats {self.i_seats}")

class Truck (Vehicle):

def __init__(self, color, capacity):

self.i_color = color

self.i_capacity = capacity

def print_me(self):

print( f"Truck with color {self.i_color} and \

loading capacity {self.i_capacity} tons")

if __name__ == "__main__":

car = Car ("blue", 5)

car.print_vehicle_info()

car.print_me()

truck = Truck("white", 1000)

truck.print_vehicle_info()

truck.print_me()

In this example, we created a Vehicle parent class with one i_color attribute and one print_vehicle_info method. Both the elements are a candidate for inheritance.

Next, we created two child classes, Car and Truck. Each child class has one additional attribute (i_seats and i_capacity) and one additional method (print_me). In the print_me methods in each child class, we access the parent class instance attribute as well as child class instance attributes.

This design was intentional, to elaborate the idea of inheriting some elements from the parent class and adding some elements of its own in a child class. The two child classes are used in this example to demonstrate the role of inheritance toward reusability. In our main program, we created Car and Truck instances and tried to access the parent method as well as the instance method. The console output of this program is as expected and is shown below:

This is vehicle and I know my color is blue

Car with color blue and no of seats 5

This is vehicle and I know my color is white

Truck with color white and loading capacity 1000 tons






Share:

0 comments:

Post a Comment