Inheritance in Object-Oriented Programming:
- Definition: Mechanism of basing a class (child class) on another class (parent class).
- Types of Inheritance:
1. Single Inheritance: One child class inherits from one parent class.
2. Multiple Inheritance: A child class inherits from multiple parent classes.
3. Multilevel Inheritance: A class is derived from another derived class.
4. Hierarchical Inheritance: Multiple child classes inherit from a single parent class.
5. Hybrid Inheritance: Combination of two or more types of inheritance.
- Benefits:
- Code Reusability.
- Easy to maintain and extend existing code.
- Example in Python:
```python
class Parent:
def greet(self):
print("Hello from Parent")
class Child(Parent):
def greet_child(self):
print("Hello from Child")
obj = Child()
obj.greet() # Output: Hello from Parent
obj.greet_child() # Output: Hello from Child
```