Inheritance in Python
Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a class (known as the child or derived class) to inherit attributes and methods from another class (known as the parent or base class). This promotes code reuse and establishes a relationship between classes.
Types of Inheritance
- Single Inheritance: A class inherits from a single parent class.
- Multiple Inheritance: A class can inherit from multiple parent classes.
- Multilevel Inheritance: A class is derived from another derived class.
- Hierarchical Inheritance: Multiple classes inherit from a single parent class.
- Hybrid Inheritance: A combination of two or more types of inheritance.
1. Single Inheritance
In single inheritance, a derived class inherits from one base class.
Code Example
class Animal:
def speak(self):
return "Animal speaks"
class Dog(Animal):
def bark(self):
return "Dog barks"
dog = Dog()
print(dog.speak()) # Inherited method
print(dog.bark()) # Method of Dog class
Output
Dog barks
2. Multiple Inheritance
In multiple inheritance, a class can inherit from more than one parent class.
Code Example
class Flyer:
def fly(self):
return "Can fly"
class Swimmer:
def swim(self):
return "Can swim"
class Duck(Flyer, Swimmer):
def quack(self):
return "Duck quacks"
duck = Duck()
print(duck.fly()) # Method from Flyer class
print(duck.swim()) # Method from Swimmer class
print(duck.quack()) # Method of Duck class
Output
Can swim
Duck quacks
3. Multilevel Inheritance
In multilevel inheritance, a class is derived from another derived class.
Code Example
class Grandparent:
def traits(self):
return "Has good traits"
class Parent(Grandparent):
def job(self):
return "Has a job"
class Child(Parent):
def play(self):
return "Child plays"
child = Child()
print(child.traits()) # Inherited from Grandparent
print(child.job()) # Inherited from Parent
print(child.play()) # Method of Child class
Output
Has a job
Child plays
4. Hierarchical Inheritance
In hierarchical inheritance, multiple classes inherit from a single base class.
Code Example
class Vehicle:
def start(self):
return "Vehicle starts"
class Car(Vehicle):
def drive(self):
return "Car drives"
class Bike(Vehicle):
def ride(self):
return "Bike rides"
car = Car()
bike = Bike()
print(car.start()) # Inherited method
print(car.drive()) # Method of Car class
print(bike.start()) # Inherited method
print(bike.ride()) # Method of Bike class
Output
Car drives
Vehicle starts
Bike rides
5. Hybrid Inheritance
Hybrid inheritance is a combination of two or more types of inheritance.
Code Example
class Animal:
def eat(self):
return "Eating"
class Bird(Animal):
def fly(self):
return "Flying"
class Fish(Animal):
def swim(self):
return "Swimming"
class Duck(Bird, Fish):
def quack(self):
return "Quacking"
duck = Duck()
print(duck.eat()) # Inherited from Animal
print(duck.fly()) # Inherited from Bird
print(duck.swim()) # Inherited from Fish
print(duck.quack()) # Method of Duck class
Output
Flying
Swimming
Quacking
Flowchart Of Inheritance
Conclusion
Inheritance in Python provides a powerful way to establish relationships between classes, allowing for code reuse and organization. By using different types of inheritance, developers can design flexible and modular applications.