Getting Started

Python - IntroductionPython - Hello World ProgramPython - SyntaxPython - Data TypesPython - Variables

Operators

Python - Arithmetic OperatorsPython - Relational OperatorsPython - Logical OperatorsPython - Assignment OperatorsPython - Bitwise OperatorsPython - Membership OperatorsPython - Identity OperatorsPython - Increment and Decrement Operators

Conditions

Python - If Else statement

Loop

Python - While LoopPython - For Loop

Numbers

Python - NumbersPython - Number Conversion

Strings

Python - StringsPython - String OperatorsPython - String FormattingPython - String MethodsPython - String Format Method

List

Python - ListPython - List Methods

Tuple

Python - Tuple

Set

Python - SetPython - Set Methods

Dictionary

Python - DictionaryPython - Dictionary Methods

Functions

Python - FunctionsPython - Functions - Variable length argumentsPython - Lambda Function

Scope of Variables

Python - Scope of Variables

Modules

Python - ModulesPython - Math ModulePython - JSON ModulePython - datetime ModulePython - time Module

I/O

Python - Read input from keyboard

File

Python - File Handling

Exception Handling

Python - Exception Handling

OOP

Python - Classes and ObjectsPython - Class Constructor __init__ methodPython - Class Destructor __del__ methodPython - Built-in Class AttributesPython - InheritancePython - Method OverridingPython - Method Overloading

Package Management

Python - PIP

Python - MySQL

Python - MySQL - Getting StartedPython - MySQL - Insert dataPython - MySQL - Select dataPython - MySQL - Update dataPython - MySQL - Delete data

Python - CSV

Python - Read data from CSV filePython - Write data in CSV file

Python - Inheritance

Python

python logo

In this tutorial we will learn about inheritance in Python.

What is inheritance?

Inheritance is one of the core concepts of OOP - Object Oriented Programming. It helps us to create hierarchy.

Inheritance is a concept were a child class inherits the properties and methods from the parent class.

Super class and Sub class

The class that is used to create other classes is called the parent class or super class.

The class that inherits the attributes and methods of the parent class is called the sub class or child class.

Inheritance in Python

Following is the syntax of inheritance in Python.

# parent class
class ParentClass:
  #
  # some attributes of the parent class
  #

  #
  # some methods of the parent class
  #

class ChildClass(ParentClass):
  #
  # some attributes of the child class
  #

  #
  # some methods of the child class
  #

Note! The attributes and methods of the ParentClass is inherited by the ChildClass.

Inheritance Example

In the following Python program we have a parent class Employee and a child class Manager.

# parent class
class Employee:

    def __init__(self, id, name):
        print("Hello from Employee.__init__()")
        self.id = id
        self.name = name

    def employeeDetail(self):
        print("ID: %s" %self.id)
        print("Name: %s" %self.name)

# child class
class Manager(Employee):

    def __init__(self, id, name, project):
        super().__init__(id, name)
        print("Hello from Manager.__init__()")
        self.project = project

    def projectDetail(self):
        print("Project: %s" %self.project)


# object of Manager class
obj = Manager(1, 'Jane Doe', 'Android App')

# output
print("-----Manager Detail-----")
obj.employeeDetail()

print("-----Project Detail-----")
obj.projectDetail()

The above code will print the following output.

Hello from Employee.__init__()
Hello from Manager.__init__()
-----Manager Detail-----
ID: 1
Name: Jane Doe
-----Project Detail-----
Project: Android App

Explanation:

We have the parent class Employee and the child class Manager which inherits attributes and methods of the parent class.

Employee class

In this class we have the __init__ method that takes two arguments id and name of the employee which we are saving using the self keyword.

Then we have the employeeDetail method that prints the detail of the employee.

Manager class

This class also includes the __init__ method and it takes three arguments id, name and project of the employee.

Using the super method in the child class we call the __init__ method of the parent class.

Inside the __init__ method of the Manager class we are calling the __init__ method of the Employee class and passing the id and name value.

We are assigning the value of project to the instance attribute project using the self keyword.

Then, there is the projectDetail method that prints out the project name.

Creating object

We create an object of the Manager class which inherits all the properties and methods of the Employee class.

While instantiating an object of the Manager class we are passing the id, name and project name of the employee.

Finally, we are calling the employeeDetail and projectDetail methods which prints the values set for the employee.