Inheritance in Python Inner Class
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):
self.name = '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
Example:
Python3
# Python program to demonstrate
# inner class
class person:
def __init__(self):
self.name = 'AKASH'
self.db = self.Dob()
def display(self):
print('NAME = ', self.name)
# this is inner class
class Dob:
def __init__(self):
self.dd = 10
self.mm = 3
self.yy = 2000
def display(self):
print('DOB = {}/{}/{}'.format(self.dd, self.mm, self.yy))
# creating person class object
p = person()
p.display()
# create inner class object
x = p.db
x.display()
Output:
NAME = AKASH
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.
Python3
# 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')
super().__init__()
class Inner(A.Inner):
def display2(self):
print('Inner Of Child Class')
# creating child class object
p = B()
p.display()
# create inner class object
x = p.db
x.display1()
x.display2()
Output:
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.
Similar Reads
Inheritance in Python Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (called a child or derived class) to inherit attributes and methods from another class (called a parent or base class). This promotes code reuse, modularity, and a hierarchical class structure. In this arti
7 min read
Inner Class in Python Python is an Object-Oriented Programming Language, everything in Python is related to objects, methods, and properties. A class is a user-defined blueprint or a prototype, which we can use to create the objects of a class. The class is defined by using the class keyword.Example of classPython# creat
5 min read
Multiple Inheritance in Python Inheritance is the mechanism to achieve the re-usability of code as one class(child class) can derive the properties of another class(parent class). It also provides transitivity ie. if class C inherits from P then all the sub-classes of C would also inherit from P. Multiple Inheritance When a class
5 min read
Data Classes in Python | Set 4 (Inheritance) Prerequisites: Inheritance In Python, Data Classes in Python | Set 3 In this post, we will discuss how DataClasses behave when inherited. Though they make their own constructors, DataClasses behave pretty much the same way as normal classes do when inherited. Python3 1== from dataclasses import data
2 min read
Inheritance in Python | Set 2 Prerequisite : basics of inheritance in Python, Inheritance, examples of object, issubclass and super There are 2 built-in functions in Python that are related to inheritance. They are: 1. isinstance(): It checks the type of an object. Its syntax is: isinstance(object_name, class_name) It would retu
4 min read
Conditional Inheritance in Python It happens most of the time that given a condition we need to decide whether a particular class should inherit a class or not, for example given a person, if he/she is eligible for an admission in a university only then they should be a student otherwise they should not be a student. Let's consider
3 min read