Inheritance is a fundamental concept in object-oriented programming (OOP) that
allows a class (child or subclass) to inherit properties and behaviors (methods) from
another class (parent or superclass). It promotes code reusability and hierarchical
relationships.
Types of Inheritance
1. Single Inheritance – A child class inherits from a single parent class.
2. Multiple Inheritance – A child class inherits from multiple parent classes.
3. Multilevel Inheritance – A child class inherits from a parent class, which
itself is a child of another class.
4. Hierarchical Inheritance – Multiple child classes inherit from a single parent
class.
5. Hybrid Inheritance – A combination of different types of inheritance.
Example in Python
# Parent class
class Animal:
def speak(self):
print("Animal makes a sound")
# Child class inheriting from Animal
class Dog(Animal):
def bark(self):
print("Dog barks")
# Creating an object of Dog
d = Dog()
d.speak() # Inherited method
d.bark() # Child class method