Why Use Inheritance in Python?
- Code Reusability: Write common functionality in the parent class and reuse it in child classes.
- Hierarchy Representation: Represent real-world hierarchies in your code.
- Extensibility: Add or override specific functionalities in the child class without modifying the parent class.
Defining Inheritance in Python
A child class is created by passing the parent class as an argument in parentheses during its definition.
Syntax:
class ParentClass:
# Parent class content
class ChildClass(ParentClass):
# Child class content
Examples of Inheritance
Basic Example
class Animal:
def sound(self):
print("This animal makes a sound")
class Dog(Animal):
def sound(self):
print("The dog barks")
# Using the classes
a = Animal()
a.sound() # Output: This animal makes a sound
d = Dog()
d.sound() # Output: The dog barks
In this example, the Dog class inherits the sound
method from the Animal class and overrides it.
Accessing Parent Class Methods
The super() function is used to call methods of the parent class from the child class.
class Vehicle:
def info(self):
print("This is a vehicle")
class Car(Vehicle):
def info(self):
super().info()
print("This is a car")
c = Car()
c.info()
# Output:
# This is a vehicle
# This is a car
Types of Inheritance in Python
1) Single Inheritance:
A child class inherits from a single parent class.
Example:
class Parent:
def show(self):
print("This is the parent class")
class Child(Parent):
def show(self):
print("This is the child class")
c = Child()
c.show() # Output: This is the child class
2) Multiple Inheritance:
A child class inherits from multiple parent classes.
Example:
class Parent1:
def feature1(self):
print("Feature 1 from Parent1")
class Parent2:
def feature2(self):
print("Feature 2 from Parent2")
class Child(Parent1, Parent2):
pass
c = Child()
c.feature1() # Output: Feature 1 from Parent1
c.feature2() # Output: Feature 2 from Parent2
3) Multilevel Inheritance:
A child class inherits from a parent class, and another class inherits from that child class.
Example:
class Grandparent:
def feature1(self):
print("Feature 1 from Grandparent")
class Parent(Grandparent):
def feature2(self):
print("Feature 2 from Parent")
class Child(Parent):
pass
c = Child()
c.feature1() # Output: Feature 1 from Grandparent
c.feature2() # Output: Feature 2 from Parent
4) Hierarchical Inheritance:
Multiple child classes inherit from a single parent class.
Example:
class Parent:
def feature(self):
print("Common feature from Parent")
class Child1(Parent):
pass
class Child2(Parent):
pass
c1 = Child1()
c2 = Child2()
c1.feature() # Output: Common feature from Parent
c2.feature() # Output: Common feature from Parent
5) Hybrid Inheritance:
A combination of two or more types of inheritance.
Example:
class Base:
def base_method(self):
print("Base class method")
class Derived1(Base):
def derived1_method(self):
print("Derived1 class method")
class Derived2(Base):
def derived2_method(self):
print("Derived2 class method")
class Derived3(Derived1, Derived2):
def derived3_method(self):
print("Derived3 class method")
d3 = Derived3()
d3.base_method() # Output: Base class method
d3.derived1_method() # Output: Derived1 class method
d3.derived2_method() # Output: Derived2 class method
d3.derived3_method() # Output: Derived3 class method
Overriding Methods in Child Classes
A child class can redefine methods of the parent class to provide specific behavior.
Example:
class Parent:
def greet(self):
print("Hello from Parent")
class Child(Parent):
def greet(self):
print("Hello from Child")
c = Child()
c.greet() # Output: Hello from Child
Advantages of Inheritance
- Code Reusability: Write once, reuse multiple times.
- Simplified Maintenance: Changes in the parent class propagate to child classes.
- Extensibility: Easy to extend existing code.
- Modularity: Enhances code readability and organization.