Python Single Inheritance

Introduction

Single inheritance is a type of inheritance in which a child class (or subclass) inherits attributes and methods from a single parent class (or superclass). This is the simplest form of inheritance and helps in promoting code reusability.
Sure, here are the text-based diagrams for all types of inheritance along with descriptions:

Single Inheritance

A single class (child class) inherits from one parent class.

  +---------+
  | Class A |
  +---------+
       |
       |
  +---------+
  | Class B |
  +---------+

Key Concepts

Parent Class (Superclass)

The class whose attributes and methods are inherited by another class.

Child Class (Subclass)

The class that inherits attributes and methods from another class.

Syntax

Defining a Parent Class

class ParentClass:
    def __init__(self, attribute1, attribute2):
        self.attribute1 = attribute1
        self.attribute2 = attribute2

    def method1(self):
        return "This is method1 from ParentClass"

Defining a Child Class

class ChildClass(ParentClass):
    def __init__(self, attribute1, attribute2, attribute3):
        super().__init__(attribute1, attribute2)  # Inheriting attributes from ParentClass
        self.attribute3 = attribute3

    def method2(self):
        return "This is method2 from ChildClass"

Example

Let’s consider a real-world example of a person and a student, where the Student class inherits from the Person class.

Parent Class: Person

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def display_info(self):
        return f"Person: {self.name}, Age: {self.age}"

Child Class: Student

class Student(Person):
    def __init__(self, name, age, student_id):
        super().__init__(name, age)  # Inheriting attributes from Person
        self.student_id = student_id

    def display_info(self):
        return f"Student: {self.name}, Age: {self.age}, Student ID: {self.student_id}"

Creating and Using Objects

# Creating an object of the Student class
student = Student("Ramesh", 20, "S12345")

# Displaying information
print(student.display_info())  # Output: Student: Ramesh, Age: 20, Student ID: S12345

Explanation

  1. Parent Class (Person): Defines common attributes (name, age) and methods (display_info) for all persons.
  2. Child Class (Student): Inherits from Person and adds an additional attribute (student_id). It also overrides the display_info method to include the student_id.

Real-World Example: Vehicle System

Let’s consider a real-world example of a vehicle system where we have a generic Vehicle class and a more specific class like Car that inherits from Vehicle.

Parent Class: Vehicle

class Vehicle:
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

    def display_info(self):
        return f"Vehicle: {self.brand} {self.model}"

Child Class: Car

class Car(Vehicle):
    def __init__(self, brand, model, seating_capacity):
        super().__init__(brand, model)  # Inheriting attributes from Vehicle
        self.seating_capacity = seating_capacity

    def display_info(self):
        return f"Car: {self.brand} {self.model}, Seating Capacity: {self.seating_capacity}"

Creating and Using Objects

# Creating an object of the Car class
my_car = Car("Toyota", "Corolla", 5)

# Displaying information
print(my_car.display_info())  # Output: Car: Toyota Corolla, Seating Capacity: 5

Explanation

  1. Parent Class (Vehicle): Defines common attributes (brand, model) and methods (display_info) for all vehicles.
  2. Child Class (Car): Inherits from Vehicle and adds an additional attribute (seating_capacity). It also overrides the display_info method to include the seating_capacity.

Conclusion

Single inheritance in Python allows a child class to inherit attributes and methods from a single parent class. This promotes code reusability and establishes a clear and simple hierarchy. By understanding and utilizing single inheritance, you can design more organized and maintainable object-oriented systems. The provided examples of a person-student relationship and a vehicle system demonstrate how single inheritance can be effectively applied in real-world scenarios.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top