Python Classes

Python Inheritance

Inheritance in Python

Python Inheritance

Inheritance allows us to define a class that inherits all the methods and properties from another class.

Parent and Child Classes

Create a child class by passing the parent class as a parameter:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    
    def introduce(self):
        return f"I'm {self.name}, {self.age} years old"

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

student = Student("Alice", 20, "S123")
print(student.introduce())