Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
228 views

Python Inheritance

This document discusses Python inheritance. It defines inheritance as deriving properties from a parent class into a child class and adding its own properties. It describes the different types of inheritance in Python including single, multiple, multilevel, hierarchical, and hybrid inheritance. It provides an example of creating a parent and child class in Python where the child inherits properties and methods from the parent using the super() function.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
228 views

Python Inheritance

This document discusses Python inheritance. It defines inheritance as deriving properties from a parent class into a child class and adding its own properties. It describes the different types of inheritance in Python including single, multiple, multilevel, hierarchical, and hybrid inheritance. It provides an example of creating a parent and child class in Python where the child inherits properties and methods from the parent using the super() function.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Python Inheritance

MILIND ANKLESHWAR
Inheritance
One of the most important feature of OOP language
Inheritance means deriving properties from parent class into child class and adding its own
properties
Inheritance allows us to define a class that inherits all the methods and properties from
another class.
Parent class is the class being inherited from, also called base class.
Child class is the class that inherits from another class, also called derived class
Types of Inheritance
Single
Multiple
Multilevel
Hierarchical
Hybrid
Single Inheritance
Single Inheritance: In single inheritance, a class is allowed to inherit from only one class. i.e. one
sub class is inherited by one base class only.
Multiple Inheritance
Multiple Inheritance is a feature of OOP where a class can inherit from more than one classes.
i.e one sub class is inherited from more than one base classes.
Multilevel Inheritance
In this type of inheritance, a derived class is created from another derived class.
Hierarchical Inheritance:
In this type of inheritance, more than one sub class is inherited from a single base class. i.e.
more than one derived class is created from a single base class
Hybrid (Virtual) Inheritance
Hybrid Inheritance is implemented by combining more than one type of inheritance. For
example: Combining Hierarchical inheritance and Multiple Inheritance.
Inheritance in Python
Create a Parent Class
Any class can be a parent class, so the syntax is the same as creating any other class:
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 Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname

def printname(self):
print(self.firstname, self.lastname)

#Use the Person class to create an object, and then execute the printname method:

x = Person("John", "Doe")
x.printname()
class Student(Person):
pass
Add the __init__() Function
So far we have created a child class that inherits the properties and methods from its parent.

We want to add the __init__() function to the child class (instead of the pass keyword).
class Student(Person):
  def __init__(self, fname, lname):
    #add properties etc.
When you add the __init__() function, the child class will no longer inherit the parent's
__init__() function.
Use the super() Function
Python also has a super() function that will make the child class inherit all the methods and
properties from its parent:
class Student(Person):
  def __init__(self, fname, lname):
    super().__init__(fname, lname)

You might also like