0% found this document useful (0 votes)
17 views12 pages

Understanding Python Inheritance Types

Python inheritance allows a class to inherit methods and properties from another class, promoting code reuse and modularity. It includes concepts like parent and child classes, the use of the __init__() method for initialization, and the super() function to access parent class methods. There are various types of inheritance such as single, multiple, multilevel, hierarchical, and hybrid inheritance.

Uploaded by

sundymaung
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)
17 views12 pages

Understanding Python Inheritance Types

Python inheritance allows a class to inherit methods and properties from another class, promoting code reuse and modularity. It includes concepts like parent and child classes, the use of the __init__() method for initialization, and the super() function to access parent class methods. There are various types of inheritance such as single, multiple, multilevel, hierarchical, and hybrid inheritance.

Uploaded by

sundymaung
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

Python Inheritance

Python Inheritance

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

This promotes code reuse, modularity, and a hierarchical class structure.

Inheritance allows us to define a class that inherits all the methods and
properties from another class.
Inheritance Example
# Parent class
class Animal:
def __init__(self, name):
[Link] = name # Initialize the name attribute

def speak(self):
pass # Placeholder method to be overridden by child classes

# Child class inheriting from Animal


class Dog(Animal):
def speak(self):
return f"{[Link]} barks!" # Override the speak method

# Creating an instance of Dog


dog = Dog("Buddy")
print([Link]()) # Output: Buddy says Woof!
Parent Class and Child Class

1. Parent Class:
This is the base class from which other classes inherit.
It contains attributes and methods that the child class can reuse.
2. Child Class:
This is the derived class that inherits from the parent class.
The syntax for inheritance is class ChildClass(ParentClass).
The child class automatically gets all attributes and methods of the
parent class unless overridden.
Create a Parent Class
Any class can be a parent class, so the syntax is the same as creating any
other class.
# A Python program to demonstrate inheritance
class Person(object):

# Constructor
def __init__(self, name, id):
[Link] = name
[Link] = id

# To check if this person is an employee


def Display(self):
print([Link], [Link])

# Driver code
emp = Person("Satyam", 102) # An Object of Person
[Link]()
Create a Child Class
To create a class that inherits the functionality from another class, send the
parent class as a parameter when creating the child 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()
__init__() Function
the __init__() method in the Employee class ensures that both inherited and
new attributes are properly initialized

# Parent Class: Person


class Person:
def __init__(self, name, idnumber):
[Link] = name
[Link] = idnumber

# Child Class: Employee


class Employee(Person):
def __init__(self, name, idnumber, salary, post):
super().__init__(name, idnumber) # Calls Person's __init__()
[Link] = salary
[Link] = post
Add Methods
If you add a method in the child class with the same name as a function in the
parent class, the inheritance of the parent method will be overridden.

# Parent Class: Person


class Person:
def __init__(self, name, idnumber):
[Link] = name
[Link] = idnumber

def display(self):
print([Link])
print([Link])
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.

# Child Class: Employee


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

def display(self):
print([Link])
print([Link])

# Child Class: Employee


class Employee(Person):
def __init__(self, name, idnumber, salary, post):
super().__init__(name, idnumber) # Using super() to call Person's
__init__()
[Link] = salary
[Link] = post
Types of Python Inheritance
Single Inheritance: A child class inherits from one parent class.
Multiple Inheritance: A child class inherits from more than one parent
class.
Multilevel Inheritance: A class is derived from a class which is also derived
from another class.
Hierarchical Inheritance: Multiple classes inherit from a single parent
class.
Hybrid Inheritance: A combination of more than one type of inheritance.
You’re now expert about
Python Inheritance

You might also like