0% found this document useful (0 votes)
2 views

Python Inheritance Concepts

The document explains Python inheritance concepts, including basic inheritance, constructor inheritance, method overriding, using super() for parent class methods, and multiple inheritance. It provides code examples for each concept, demonstrating how classes can inherit attributes and methods from parent classes. The document highlights the use of the super() function to call parent class methods and showcases a scenario of multiple inheritance with a Manager class inheriting from both Person and Employee.

Uploaded by

yoeurthsaiyann20
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Inheritance Concepts

The document explains Python inheritance concepts, including basic inheritance, constructor inheritance, method overriding, using super() for parent class methods, and multiple inheritance. It provides code examples for each concept, demonstrating how classes can inherit attributes and methods from parent classes. The document highlights the use of the super() function to call parent class methods and showcases a scenario of multiple inheritance with a Manager class inheriting from both Person and Employee.

Uploaded by

yoeurthsaiyann20
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Inheritance Concepts

1. Basic Inheritance
Inheritance allows a class to inherit methods and attributes from another class.

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

def introduce(self):
print(f"Hello, my name is {self.name} and I am {self.age} years
old.")

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

def work(self):
print(f"{self.name} is working as a {self.position}.")

emp = Employee("John", 30, "Engineer")


emp.introduce() # Inherited from Person
emp.work() # Defined in Employee

2. Constructor Inheritance (`__init__` method)


The child class can inherit the constructor from the parent class, and the parent constructor can be
called using `super()`.

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

class Employee(Person):
def __init__(self, name, age, position):
super().__init__(name, age) # Initialize Person's attributes
self.position = position

emp = Employee("Alice", 28, "Manager")


print(emp.name) # Alice
print(emp.position) # Manager
3. Method Overriding
The child class can override the methods of the parent class to provide its own implementation.

class Person:
def introduce(self):
print("Hello, I am a person.")

class Employee(Person):
def introduce(self): # Overriding the method
print(f"Hello, my name is {self.name} and I work here.")

emp = Employee()
emp.name = "Bob"
emp.introduce() # Overridden method in Employee

4. Using `super()` for Parent Class Methods


The `super()` function allows you to call methods from the parent class inside the child class.

class Person:
def introduce(self):
print(f"Hello, I am {self.name}")

class Employee(Person):
def introduce(self):
super().introduce() # Call the parent method
print(f"I work as a {self.position}")

emp = Employee()
emp.name = "David"
emp.position = "Developer"
emp.introduce() # Calls both Person's and Employee's methods

5. Multiple Inheritance
A class can inherit from multiple classes. Here, the `Manager` class inherits from both `Person` and
`Employee`.

class Person:
def introduce(self):
print(f"Hello, I am {self.name}")

class Employee:
def work(self):
print(f"{self.name} is working.")
class Manager(Person, Employee):
def manage(self):
print(f"{self.name} is managing the team.")

mgr = Manager()
mgr.name = "Eve"
mgr.introduce() # Inherited from Person
mgr.work() # Inherited from Employee
mgr.manage() # Defined in Manager

You might also like