Single inheritance in Python refers to a class that inherits from one parent class. In this case, the subclass (child class) inherits all the attributes and methods from the parent class, and can also define its own unique attributes or methods.
Key Concepts of Single Inheritance:
Parent Class: The class that is inherited from (also called the base class).
Child Class: The class that inherits from the parent class (also called the derived class).
Method Overriding: The ability for the child class to provide a specific implementation of a method that is already defined in the parent class.
Syntax for Single Inheritance:
class ParentClass:
# Parent class code
def parent_method(self):
print("This is the parent method.")
class ChildClass(ParentClass):
# Child class code
def child_method(self):
print("This is the child method.")
How Single Inheritance Works:
The child class inherits all public methods and attributes of the parent class.
The child class can override methods of the parent class to provide a customized implementation.
The child class can also define its own new methods and attributes that are not present in the parent class.
Example of Single Inheritance:
# Parent class
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print(f"{self.name} makes a sound")
# Child class that inherits from Animal
class Dog(Animal):
def speak(self):
print(f"{self.name} barks")
# Creating an instance of Dog (child class)
dog = Dog("Buddy")
dog.speak() # Output: Buddy barks
Explanation:
The Dog class inherits from the Animal class.
The speak() method in the Dog class overrides the speak() method from the Animal class. This means that when we call dog.speak(), the speak() method from Dog is executed, not from Animal.
The name attribute is inherited from the Animal class.
Example: Using Constructor (__init__) in Single Inheritance
In the case of single inheritance, you can use the super() function to call the constructor of the parent class, and initialize its attributes in the child class.
# Parent class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def display(self):
print(f"Name: {self.name}, Age: {self.age}")
# Child class that inherits from Person
class Employee(Person):
def __init__(self, name, age, job_title):
super().__init__(name, age) # Calling the constructor of the parent class
self.job_title = job_title
def display_employee_info(self):
print(f"{self.name} is {self.age} years old and works as a {self.job_title}")
# Creating an instance of Employee (child class)
emp = Employee("John", 30, "Software Engineer")
emp.display_employee_info() # Output: John is 30 years old and works as a Software Engineer
Explanation:
The Employee class inherits from the Person class.
The __init__() method in the Employee class uses super().__init__(name, age) to call the parent class's constructor and initialize name and age attributes.
The Employee class adds a new attribute job_title and has its own method display_employee_info().
Benefits of Single Inheritance:
Code Reusability: The child class can reuse the functionality of the parent class.
Simpler Design: Single inheritance simplifies the design as there’s only one parent class, making it easier to understand the relationship between classes.
Easy to Extend: You can easily extend the functionality of the parent class without modifying its code by creating a child class.
Summary:
Single inheritance allows a child class to inherit methods and attributes from a single parent class.
The child class can override parent methods and define new ones.
The super() function can be used to call the parent class's methods, including the constructor.
Single inheritance promotes code reusability, simpler designs, and extensibility in object-oriented programming.
Let me know if you need more details or examples! 😊
0 Comments