NEXT TUTORIAL >> Instance variable & Instance method
Inner Class or Nested Class comes into existence when we define a class inside another class.
Consider two classes, A and B, and assume that class B is defined within class A, in such cases, class B is known as Nested Class or Inner Class.
class A:
class B:
In the above code, class B is the inner class or nested class defined under class A.
Let’s see one example of Nested class :
We are defining two classes, namely Employee and Skill.
Idea is to make Skill class as an inner class and Employee as an outer class.
class Employee: # outer class
def __init__(self, name, mail):
self.name = name
self.mail = mail
def display(self):
print('Employee name : ', self.name)
print('Employee mail : ', self.mail)
class Skill: # inner class
def __init__(self, skill):
self.skill = skill
def display(self):
print('Employee skill : ', self.skill)
emp = Employee('Shrey','[email protected]')
emp.display()
skill = emp.Skill('MERN| Py-Dj| RWD') # Instantiating inner class with instance of outer class
skill.display()
O/P
Employee name : Shrey
Employee mail : [email protected]
Employee skill : MERN| Py-Dj| RWD
That’s all here.
NEXT TUTORIAL >> Instance variable & Instance method
