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
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.
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.
- Inheritance in Python | Set 2
- Inheritance in Python
- Types of inheritance Python
- Multiple inheritance in Python
- Data Classes in Python | Set 4 (Inheritance)
- Python | super() in single inheritance
- Python | super() function with multilevel inheritance
- Method resolution order in Python Inheritance
- OOP in Python | Set 3 (Inheritance, examples of object, issubclass and super)
- self in Python class
- Inner Class in Python
- Wrapper Class in Python
- First Class functions in Python
- Class as decorator in python
- Python Bokeh - Colors Class
- How to Get a List of Class Attributes in Python?
- Class or Static Variables in Python
- How to create an empty class in Python?
- Changing Class Members in Python
- Extend Class Method in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.