What is inheritance in Python?
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and behaviors (methods) from another class. In Python, it promotes code reusability, extensibility, and the creation of a hierarchical structure among classes.
What is Inheritance?
At its core, inheritance means that a new class, called the 'child' or 'subclass', can acquire the attributes and methods of an existing class, known as the 'parent' or 'superclass'. This creates an 'is-a' relationship; for example, a 'Dog' 'is a' 'Animal'.
Key Concepts
- Parent Class (Superclass / Base Class): The class from which other classes inherit.
- Child Class (Subclass / Derived Class): The class that inherits from another class. It can add new attributes and methods or override existing ones.
- Method Overriding: When a child class provides its own implementation for a method that is already defined in its parent class.
super()function: A built-in function used to call methods and access attributes of the parent class from the child class.isinstance()andissubclass(): Functions to check if an object is an instance of a class, or if a class is a subclass of another class, respectively.
How to Implement Inheritance
To create a child class that inherits from a parent class, you simply specify the parent class name in parentheses after the child class name.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound."
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name) # Call the parent class constructor
self.breed = breed
def bark(self):
return f"{self.name} barks loudly!"
# Creating objects
my_animal = Animal("Generic Animal")
my_dog = Dog("Buddy", "Golden Retriever")
print(my_animal.speak()) # Output: Generic Animal makes a sound.
print(my_dog.speak()) # Output: Buddy makes a sound. (Inherited method)
print(my_dog.bark()) # Output: Buddy barks loudly! (Child's own method)
Method Overriding
Child classes can provide their own specific implementation of a method that is already defined in the parent class. When this method is called on an object of the child class, the child's version is executed.
class Vehicle:
def drive(self):
return "Vehicle is moving."
class Car(Vehicle):
def drive(self):
return "Car is driving on the road."
class Bicycle(Vehicle):
def drive(self):
return "Bicycle is pedaled by a rider."
my_car = Car()
my_bike = Bicycle()
print(my_car.drive()) # Output: Car is driving on the road.
print(my_bike.drive()) # Output: Bicycle is pedaled by a rider.
Benefits of Inheritance
- Code Reusability: Share common code among different classes, reducing redundancy.
- Extensibility: Easily extend existing functionalities without modifying the original code.
- Maintainability: Changes in the base class are propagated to all derived classes, simplifying updates.
- Polymorphism: Allows objects of different classes to be treated as objects of a common base class, enabling flexible and generic code.