0% found this document useful (0 votes)
3 views8 pages

Inheritance Python

Uploaded by

uchetrika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Inheritance Python

Uploaded by

uchetrika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

INHERITANCE-PYTHON

Inheritance is a fundamental concept in object-oriented


programming (OOP) that allows a class (called a child or derived class) to
inherit attributes and methods from another class (called a parent or base
class). In this article, we'll explore inheritance in Python.

Syntax for Inheritance


class ParentClass:
# Parent class code here
pass
class ChildClass(ParentClass):
# Child class code here
pass

Explanation of Syntax

Parent Class:
 This is the base class from which other classes inherit.
 It contains attributes and methods that the child class can reuse.

Child Class:
 This is the derived class that inherits from the parent class.
 Syntax for inheritance is class ChildClass(ParentClass).
 Child class automatically gets all attributes and methods of the
parent class unless overridden.

Why do we need Inheritance in Python


 Promotes code reusability by sharing attributes and methods
across classes.
 Models real-world hierarchies like Animal → Dog or Person →
Employee.
 Simplifies maintenance through centralized updates in parent
classes.
 Enables method overriding for customized subclass behavior.
 Supports scalable, extensible design using polymorphism.

ELURI RAMESH 1
Creating a Parent Class
In object-oriented programming, a parent class (also known as a base
class) defines common attributes and methods that can be inherited by
other classes. These attributes and methods serve as the foundation for
the child classes. By using inheritance, child classes can access and extend
the functionality provided by the parent class.
Here's an example where Person is the parent class:

# A Python program to demonstrate inheritance


class Person(object):

# Constructor
def __init__(self, name, id):
self.name = name
self.id = id

# To check if this person is an employee


def Display(self):
print(self.name, self.id)

# Driver code
emp = Person("Elon", 102) # An Object of Person
emp.Display()

Output
Elon 102

Explanation
 The Person class has two attributes: name and id. These are set
when an object of the class is created.
 The display method prints the name and id of the person.

ELURI RAMESH 2
Creating a Child Class
A child class (also known as a subclass) is a class that inherits properties
and methods from its parent class. The child class can also introduce
additional attributes and methods, or even override the ones inherited
from the parent.

In this case, Emp is the child class that inherits from the Person class:

class Emp(Person):

def Print(self):
print("Emp class called")

Emp_details = Emp("Mayank", 103)

# calling parent class function


Emp_details.Display()

# Calling child class function


Emp_details.Print()

Explanation:
 Emp class inherits the name and id attributes and the display
method from the Person class.
 __init__ method in Emp calls super().__init__(name, id) to invoke
the constructor of the Person class and initialize the inherited
attributes.

ELURI RAMESH 3
super() Function
super() function is used to call the parent class’s methods. In particular, it
is commonly used in the child class’s __init__() method to initialize
inherited attributes. This way, the child class can leverage the functionality
of the parent class.

Example:

# Parent Class: Person


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

def display(self):
print(self.name)
print(self.idnumber)

# Child Class: Employee


class Employee(Person):
def __init__(self, name, idnumber, salary, post):
super().__init__(name, idnumber) # Using super() to call Person's __init__()
self.salary = salary
self.post = post

Explanation:
 The super() function is used inside the __init__() method of
Employee to call the constructor of Person and initialize the
inherited attributes (name and idnumber).
 This ensures that the parent class functionality is reused without
needing to rewrite the code in the child class.

ELURI RAMESH 4
Add Properties
Once inheritance is established, both the parent and child classes can
have their own properties. Properties are attributes that belong to a class
and are used to store data.

Example:

# Parent Class: Person


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

def display(self):
print(self.name)
print(self.idnumber)

# Child Class: Employee


class Employee(Person):
def __init__(self, name, idnumber, salary, post):
super().__init__(name, idnumber)
self.salary = salary
self.post = post

Explanation:
 Person class has properties name and idnumber.
 Employee class adds properties salary and post.
 The properties are initialized when an object is created, and they
represent the specific data related to the Person and Employee.

ELURI RAMESH 5
Types of Python Inheritance
1. Single Inheritance: A child class inherits from one parent class.
2. Multiple Inheritance: A child class inherits from more than one
parent class.
3. Multilevel Inheritance: A class is derived from a class which is
also derived from another class.
4. Hierarchical Inheritance: Multiple classes inherit from a single
parent class.
5. Hybrid Inheritance: A combination of more than one type of
inheritance.

Example:
# 1. Single Inheritance
class Person:
def __init__(self, name):
self.name = name

class Employee(Person): # Employee inherits from Person


def __init__(self, name, salary):
super().__init__(name)
self.salary = salary

# 2. Multiple Inheritance
class Job:
def __init__(self, salary):
self.salary = salary

class EmployeePersonJob(Employee, Job): # Inherits from both Employee and Job


def __init__(self, name, salary):
Employee.__init__(self, name, salary) # Initialize Employee
Job.__init__(self, salary) # Initialize Job

# 3. Multilevel Inheritance
class Manager(EmployeePersonJob): # Inherits from EmployeePersonJob
def __init__(self, name, salary, department):
EmployeePersonJob.__init__(self, name, salary) # Explicitly initialize
EmployeePersonJob
self.department = department

# 4. Hierarchical Inheritance
class AssistantManager(EmployeePersonJob): # Inherits from EmployeePersonJob
def __init__(self, name, salary, team_size):
EmployeePersonJob.__init__(self, name, salary) # Explicitly initialize
EmployeePersonJob

ELURI RAMESH 6
self.team_size = team_size

# 5. Hybrid Inheritance (Multiple + Multilevel)


class SeniorManager(Manager, AssistantManager): # Inherits from both Manager and
AssistantManager
def __init__(self, name, salary, department, team_size):
Manager.__init__(self, name, salary, department) # Initialize Manager
AssistantManager.__init__(self, name, salary, team_size) # Initialize
AssistantManager

# Creating objects to show inheritance

# Single Inheritance
emp = Employee("John", 40000)
print(emp.name, emp.salary)

# Multiple Inheritance
emp2 = EmployeePersonJob("Alice", 50000)
print(emp2.name, emp2.salary)

# Multilevel Inheritance
mgr = Manager("Bob", 60000, "HR")
print(mgr.name, mgr.salary, mgr.department)

# Hierarchical Inheritance
asst_mgr = AssistantManager("Charlie", 45000, 10)
print(asst_mgr.name, asst_mgr.salary, asst_mgr.team_size)

# Hybrid Inheritance
sen_mgr = SeniorManager("David", 70000, "Finance", 20)
print(sen_mgr.name, sen_mgr.salary, sen_mgr.department, sen_mgr.team_size)

Output
John 40000
Alice 50000
Bob 60000 HR
Charlie 45000 10
David 70000 Finance 20

ELURI RAMESH 7
Explanation:
1. Single Inheritance: Employee inherits from Person, adding a
salary attribute.
2. Multiple Inheritance: EmployeePersonJob inherits from both
Employee and Job, allowing access to both name and salary.
3. Multilevel Inheritance: Manager inherits from
EmployeePersonJob, which already includes Employee and Job.
4. Hierarchical Inheritance: AssistantManager also inherits from
EmployeePersonJob, demonstrating multiple child classes
inheriting from the same parent.
5. Hybrid Inheritance: SeniorManager inherits from both Manager
(multilevel) and AssistantManager (hierarchical), combining two
inheritance types.

ELURI RAMESH 8

You might also like