Inheritance
Inheritance is a way to create a new class that inherits the methods and properties of the existing class.
The existing class is said to be the base class or the parent class, and the new class is said to be the derived class or the child class.
Syntax:
class parent:
pass
class child(parent):
pass
Creating a Parent Class
The parent class is created in the same way as we normally create a class in python.
class car:
def __init__(self, brand, seating_capacity):
self.brand = brand
self.seating_capacity = seating_capacity
def car_details(self):
print("This is a car.")
Creating a Child Class
The parent class is sent as the parameter in parentheses while creating the child class.
Example:
class car:
brand = ""
def car_details(self):
print("This is a car.")
class electric_car(car):
def electric_car_details(self):
print("This is a electric car. Made By", self.brand)
e_car = electric_car()
e_car.brand = "Ford"
e_car.car_details()
e_car.electric_car_details()
Calling Constructor of Parent Class
From the child class, we can call the constructor of the parent class, means we can call the __init__() method of the parent class from the __init__ method of the child class. Like this, we can initialize the variables of the parent class from the child class.
Syntax:
<parentclassname>.__init__(self, <parameters>)
Example:
class car:
def __init__(self, brand, seating_capacity):
self.brand = brand
self.seating_capacity = seating_capacity
def car_details(self):
print("This is a car.")
class electric_car(car):
def __init__(self, model, cartype, brand, seating_capacity):
car.__init__(self, brand, seating_capacity)
self.model = model
self.cartype = cartype
def elec_car_details(self):
print("This is an electric car.")
model_3 = electric_car("Model 3", "Sedan", "Tesla", 5)
model_3.model
model_3.car_details()
model_3.brand
Method Overriding
A subclass has all the methods of the parent class, but in some cases a subclass may have different requirements than the parent class. In this case, a new method with the same name and same parameters can be defined in the child class with additional functionalities. This is called method overriding. Means method of the child class with the same name and parameters will override the method of the parent class.
class shape:
def draw(self):
print("Draw a shape.")
class circle(shape):
def draw(self):
print("Draw a circle.")
shape1 = circle()
shape1.draw()
The super() Method
In the child class, if we want to use the method of the super class, then the super() method is used.
class shape:
def draw(self):
print("Draw a shape.")
class circle(shape):
def draw(self):
super().draw()
print("Draw a circle.")
shape1 = circle()
shape1.draw()
Advantages of Inheritance
1. Code Reusability : Once the baser class code is written and tested properly, we can reuse in the derived classes.
2. Easy to modify code : To make changes in the derived classes, we can simply make changes in the parent class, which will automatically be reflected in the derived class.
3. Consistent behavior : When a parent class is inherited by multiple classes, it ensures the same behavior in all the child classes.
4. Saves development time : As we are re-using the already written code, so it saves time of the developers as they need not rewrite the same code.
5. Abstraction : As we are not concerted about the code implementation of the base class, so it provides the abstraction for the base class code.
Disadvantages of Inheritance
1. Increases Code Complexity : Improper or the overuse of the inheritance can increase the complexity of the code.
2. Need to understand the hierarchy : To make inheritance work properly and give the desired result, user needs to understand the hierarchy properly, otherwise, chances that that user can mess the results.
Types of inheritance:
- Single Inheritance : A child class is derived from a single parent class.
- Multiple Inheritance : A child class is derived from more than one base class.
- Multilevel Inheritance : A class is derived from a child class.
- Hierarchical Inheritance : When more than one child class is inherited from a single base class.
- Hybrid Inheritance : Combination of multiple types of inheritance.