Inheritance is one of the important concepts of object-oriented programming (OOP). Python, being a versatile and widely-used programming language, seamlessly integrates the concept of inheritance to empower developers to create a new class based on an existing class. This established relationship allows the derived class (often termed the "child" or "subclass") to inherit attributes and behavior from the base class (frequently called the "parent" or "superclass"). This leads to a natural "is-a" relationship, denoting that a derived class is a specific instantiation of its base class.
Syntax:
class DerivedClassName(BaseClassName):
pass
For illustration purposes, let's delve into an example:
class Shape:
def __init__(self, width, height):
self.width = width
self.height = height
def set_dimensions(self, w, h):
self.width = w
self.height = h
class Rectangle(Shape):
def area(self):
return self.width * self.height
# Usage
rect = Rectangle(4, 5)
print(rect.area()) # Output: 20
In the above example, `Rectangle` is a subclass of `Shape`. It inherits the properties `width` and `height` and the method `set_dimensions` from its superclass, `Shape`. The `Rectangle` class further extends the functionality by introducing the `area` method.
The `__init__()` Function in Child Class:
In Python, when you create a child class (subclass) that inherits from a parent class (superclass), the child class can have its own constructor, called the `__init__()` method. This method is responsible for initializing the properties of the child class, and it can also override the `__init__()` method of the parent class.
When you define an `__init__()` method in the child class, it overrides the parent class's `__init__()` method. However, if you still want to use the parent class's `__init__()` functionality along with the child class's specific properties, you can call the parent class's `__init__()` method explicitly using the `super()` function.
Here's an example to illustrate this:
class Parent:
def __init__(self, parent_var):
self.parent_var = parent_var
class Child(Parent):
def __init__(self, parent_var, child_var):
super().__init__(parent_var) # Calling parent class's __init__()
self.child_var = child_var
# Creating instances
parent_obj = Parent("Parent variable")
child_obj = Child("Parent variable", "Child variable")
print(parent_obj.parent_var) # Output: Parent variable
print(child_obj.parent_var) # Output: Parent variable
print(child_obj.child_var) # Output: Child variable
In the example above:
- The `Parent` class has an `__init__()` method that initializes the `parent_var` attribute.
- The `Child` class inherits from the `Parent` class and also has an `__init__()` method. The `super().__init__(parent_attr)` line explicitly calls the `__init__()` method of the parent class to initialize the `parent_var`.
- When you create an instance of the `Child` class, both the parent and child attributes are properly initialized.
Python makes it possible to refer to the parent class explicitly using the `super()` function. This is especially handy when you override methods but still want to include the method from the parent class.
Types of Inheritance:
Here are the types of inheritance supported in Python:
1. Single Inheritance:
In single inheritance, a class inherits from a single base class. It's the simplest form of inheritance.
class Base:
pass
class Derived(Base):
pass
2. Multiple Inheritance:
This type allows a class to inherit from more than one base class.
class Base1:
pass
class Base2:
pass
class Derived(Base1, Base2):
pass
3. Multilevel Inheritance:
In multilevel inheritance, a class is derived from a class which is itself derived from another class, forming a chain of inheritance.
class Grandparent:
pass
class Parent(Grandparent):
pass
class Child(Parent):
pass
4. Hierarchical Inheritance:
In hierarchical inheritance, multiple classes inherit from a single class, making it the central base class for various derived classes.
class Base:
pass
class Derived1(Base):
pass
class Derived2(Base):
pass
5. Hybrid Inheritance:
Hybrid inheritance is a combination of more than one type of inheritance. For example, a combination of multiple and multilevel inheritances would be considered hybrid inheritance.
class Base:
pass
class Derived1(Base):
pass
class Derived2(Base):
pass
class Derived3(Derived1, Derived2):
pass