Skip to content
Related Articles

Related Articles

Inheritance in Python Inner Class

View Discussion
Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 30 Jan, 2020

A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.

An inner class or nested class is defined inside the body of another class. If an object is created using a class, the object inside the root class can be used. A class can have one or more than one inner class. Inner class or nested class helps the user in many ways such as the logical grouping of classes, more readable and easily maintainable, etc.

For example, consider a situation where a class DOB is inside another class Person and see the below code snippet.

class person:
    def __init__(self): = 'AKASH'
        self.db = self.Dob()   #this is Dob object

In the preceding code, ’db.’ represent the inner class object. When the outer class object is created, it contains a subobject that is an inner class object. Hence, we can refer outer class and inner class members as:

p = person()  #create outer class object
p.display()   #call outer class method

x = p.db     #create inner class object
x.display()    #call inner class method


# Python program to demonstrate
# inner class
class person:
    def __init__(self): = 'AKASH'
        self.db = self.Dob()
    def display(self):
        print('NAME = ',
    # this is inner class
    class Dob:
        def __init__(self):
            self.dd = 10
   = 3
            self.yy = 2000
        def display(self):
            print('DOB = {}/{}/{}'.format(self.dd,, self.yy))
# creating person class object
p = person()
# create inner class object
x = p.db


DOB = 10/3/2000

Inheritance in Inner Class

Inheritance is the capability of one class to derive or inherit the properties from some another class. The benefits of inheritance are:

  • It represents real-world relationships well.
  • It provides reusability of a code. We don’t have to write the same code again and again. Also, it allows us to add more features to a class without modifying it.
  • It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class A.

To use inheritance in the inner class, consider the below code snippet.

# Python program to demonstrate
# inheritance in inner class
class A:
    def __init__(self):
        self.db = self.Inner()
    def display(self):
        print('In Parent Class')
    # this is inner class
    class Inner:
        def display1(self):
            print('Inner Of Parent Class')
class B(A):
    def __init__(self):
        print('In Child Class')
    class Inner(A.Inner):
        def display2(self):
            print('Inner Of Child Class')
# creating child class object
p = B()
# create inner class object
x = p.db


In Child Class
In Parent Class
Inner Of Parent Class
Inner Of Child Class

In the above example, Class B inherits from A and the inner class of B inherits from the inner class of A. Then the class methods of Parent’ Inner class are called from the child’s inner class object.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!