Inheritence
Inherit properties and methods from another class.
Inheritance in Python allows a class (called a child class or subclass) to inherit attributes and methods from another class (called a parent class or superclass). This enables code reusability and establishes relationships between classes.
class ParentClass:
# Parent class methods and attributes
class ChildClass(ParentClass):
# Child class inherits from ParentClass
# Can override or extend ParentClass functionality
Key Concepts:
- Parent Class (Superclass): The class whose properties and methods are inherited.
- Child Class (Subclass): The class that inherits from the parent class.
- Method Overriding: A child class can provide a specific implementation of a method defined in the parent class.
super()
Function: Used to call a method from the parent class within the child class.
Example 1: Basic Inheritance
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound."
class Dog(Animal): # Dog inherits from Animal
def speak(self):
return f"{self.name} barks."
# Create objects
animal = Animal("Animal")
dog = Dog("Buddy")
print(animal.speak()) # Output: Animal makes a sound.
print(dog.speak()) # Output: Buddy barks.
Example 2: Using super()
to Call the Parent Class
class Vehicle:
def __init__(self, make, model):
self.make = make
self.model = model
def display_info(self):
return f"Vehicle: {self.make} {self.model}"
class Car(Vehicle):
def __init__(self, make, model, year):
super().__init__(make, model) # Call parent constructor
self.year = year
def display_info(self):
base_info = super().display_info() # Call parent method
return f"{base_info}, Year: {self.year}"
# Create a Car object
car = Car("Toyota", "Corolla", 2022)
print(car.display_info()) # Output: Vehicle: Toyota Corolla, Year: 2022
Example 3: Multilevel Inheritance
When a child class inherits a class which is child class of some other class.
class Animal:
def speak(self):
return "Animal speaks."
class Mammal(Animal):
def speak(self):
return "Mammal speaks."
class Dog(Mammal):
def speak(self):
return "Dog barks."
# Multilevel inheritance
dog = Dog()
print(dog.speak()) # Output: Dog barks
Example 4: Multiple Inheritance: when a child class inherits from multiple parent classes.
class Parent1:
def feature1(self):
return "Feature 1 from Parent1"
class Parent2:
def feature2(self):
return "Feature 2 from Parent2"
class Child(Parent1, Parent2):
pass
# Multiple inheritance:
child = Child()
print(child.feature1()) # Output: Feature 1 from Parent1
print(child.feature2()) # Output: Feature 2 from Parent2
Types of Inheritance in Python:
- Single Inheritance: One child class inherits from one parent class.
- Multiple Inheritance: One child class inherits from multiple parent classes.
- Multilevel Inheritance: A chain of inheritance (A → B → C).
- Hierarchical Inheritance: Multiple child classes inherit from a single parent class.
- Hybrid Inheritance: A combination of two or more types of inheritance.
Advantages of Inheritance:
- Promotes code reusability.
- Helps organize code into a hierarchy of related classes.
- Simplifies maintenance by centralizing shared functionality.
No questions available.