Inheritance in Python | OOP's

Author: neptune | 27th-Jan-2023
#Python

Introduction to Inheritance

1. Inheritance describes is a kind of relationship between two or more classes, In general getting and storing specific details in the subclass from super class.

2. To create a child class, specify the parent class name inside the pair of parenthesis, followed by it's name.

Example :
Class ChildClass(ParentClass):   

    pass     


3. Every child class inherits all the behaviors exhibited by their parent class.


4. In Python, every class uses inheritance and is inherited from an object by default.

5. Hence, the below two definitions of MySubClass are the same.

Definition 1

class MySubClass:   

   pass


Definition 2

class MySubClass(object):   

    pass                    


1. “object” is known as parent or super class.

2. “MySubClass” is known as child or subclass or derived class.

Inheritance in Action

class Person:

    def __init__(self, fname, lname):

        self.fname = fname

        self.lname = lname

class Employee(Person):

    all_employees = []

    def __init__(self, fname, lname, empid):

        Person.__init__(self, fname, lname)

        self.empid = empid

        Employee.all_employees. append(self)


Employee” class is derived from “Person”.

p1 = Person('George', 'smith')

print(p1.fname, '-', p1.lname)


e1 = Employee('Jack', 'simmons', 456342)

e2 = Employee('John', 'williams', 123656)

print(e1.fname, '-', e1.empid)

print(e2.fname, '-', e2.empid)


Output:

George - smith

Jack - 456342

John - 123656


1. In the above example, Employee class utilizes __init __ method of the parent class Person to create its object.

2. Inheritance feature can be also used to extend the built-in classes like list or dict.

3. The following example extends list and creates EmployeesList, which can identify employees, having a given search word in their first name.


Example 1 :

class EmployeesList(list):

    def search(self, name):

        matching_employees = []

        for employee in Employee.all_employees:

            if name in employee.fname:

                matching_employees.append(employee.fname)

        return matching_employees

Extending Built-in Types EmployeesList object can be used to store all employee objects, just by replacing statement all_employees = [] with all_employees = EmployeesList().


Example 2

class Employee(Person):

    all_employees = EmployeesList()

    def __init__(self, fname, lname, empid):

        Person.__init__(self, fname, lname)

        self.empid = empid

        Employee.all_employees.append(self)


e1 = Employee('Jack', 'simmons', 456342)

e2 = Employee('George', 'Brown', 656721)

print(Employee.all_employees.search('or'))


Output :

['George']


If you have questions, or simply would like to share your thoughts, then feel free to reach out in the comments section below.


Other related blogs:
Classes and Objects in Python 3 | OOP's

Input and Output in Python

Datatypes in Python.

Core Python Syllabus for Interviews

How to reverse string in Python ?

What exactly you can do with Python?
Getting started with Jupyter Notebook | Python




Related Blogs
How to extract Speech from Video using Python?
Author: neptune | 16th-Jun-2023
#Python #Projects
Simple and easy way to convert video into audio then text using Google Speech Recognition API...

How to download video from youtube using python module ?
Author: neptune | 15th-Jun-2023
#Python
We will let you know how you can easily download the Youtube high quality videos along with subtitle, thumbnail, description using python package..

Deploy Django project on AWS with Apache2 and mod_wsgi module.
Author: neptune | 18th-May-2024
#Python #Django
In this blog I use the AWS Ubuntu 18.22 instance as Hosting platform and used Apache2 server with mod_wsgi for configurations. We create a django sample project then configure server...

Best Python package manager and package for virtual environment ?
Author: neptune | 18th-Jun-2023
#Python #Pip
We will explore the options of Pip, Virtualenv, Anaconda, and also introduce Pyenv as a helpful tool...

Mostly asked Python Interview Questions - 2023.
Author: neptune | 30th-May-2023
#Python #Interview
Python interview questions for freshers. These questions asked in 2022 Python interviews...

Core Python Syllabus for Interviews
Author: neptune | 26th-Jul-2023
#Python #Interview
STRING MANIPULATION : Introduction to Python String, Accessing Individual Elements, String Operators, String Slices, String Functions and Methods...

Python Built-in functions lambda, map, filter, reduce.
Author: neptune | 15th-Jun-2023
#Python
We are going to explore in deep some important Python build-in functions lambda, map, filter and reduce with examples...

How to reverse string in Python ?
Author: neptune | 16th-May-2022
#Python
We are going to explore different ways to reverse string in Python...

Python 3.9 new amazing features ?
Author: neptune | 26th-Jul-2023
#Python
Python 3.9 introduces new features such as dictionary union, string methods to remove prefixes and suffixes, type hinting, and speed improvements for built-in functions...

10 Proven Ways to Earn Money Through Python
Author: neptune | 11th-Apr-2023
#Python
Python offers numerous earning opportunities from web development to teaching, data analysis, machine learning, automation, web scraping, and more...

5 Languages that Replace Python with Proof
Author: neptune | 13th-Apr-2023
#Python
Julia, Rust, Go, Kotlin, and TypeScript are modern languages that could replace Python for specific use cases...

Monkey Patching in Python: A Powerful Yet Controversial Technique
Author: neptune | 01st-Aug-2023
#Python
Monkey patching in Python is a dynamic technique to modify code at runtime. It can add/alter behavior, but use it judiciously to avoid maintainability issues...

Building a Simple Chatbot with Python and openpyxl
Author: neptune | 25th-Jun-2024
#Python #Projects
This chatbot reads questions and answers from an Excel file and provides responses based on user input...

Best Practices for Managing Requests Library Sessions When Interacting with Multiple APIs ?
Author: neptune | 22nd-Aug-2024
#Python
When working with Python's `requests` library, managing sessions is crucial, especially when your application interacts with multiple APIs...

How to Update XML Files in Python?
Author: neptune | 01st-Jul-2024
#Python
Handling XML files in Python is straightforward with the `xml.etree.ElementTree` module...

How to Ensure Proper Namespace Handling in XML with Python's lxml Library
Author: neptune | 01st-Jul-2024
#Python
By using `lxml`, you can effectively manage XML namespaces and ensure that your XML structure remains intact during updates...

View More