0% found this document useful (0 votes)
22 views

40.inner Classes (ADV)

Inner classes, also called nested classes, allow defining a class within another class. This groups related data and can improve encapsulation. The document provides two examples of inner classes: 1. An Employee class with a nested Doj (Date of Joining) class to store the employee's joining date. 2. A Student class with nested Dob (Date of Birth) and Marks classes to store the student's details like name, dob, and marks in different subjects.

Uploaded by

Dosapati Prasuna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

40.inner Classes (ADV)

Inner classes, also called nested classes, allow defining a class within another class. This groups related data and can improve encapsulation. The document provides two examples of inner classes: 1. An Employee class with a nested Doj (Date of Joining) class to store the employee's joining date. 2. A Student class with nested Dob (Date of Birth) and Marks classes to store the student's details like name, dob, and marks in different subjects.

Uploaded by

Dosapati Prasuna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SSST Computer Education

Besides R.S.Brothers Show Room


Kphb- Hyderabad - 9866144861
Python
Inner Classes or nested Classes
 Process of defining a class or classes inside another class
 Advantages are grouping or sub grouping of data

Syn:
class <className>:
Attributes [Fields | Methods | constructors ]
class <InnerClass>:
Attributes [Fields | Methods | Constructors ]

Example:

class Employee:
def setEmployee(self):
self.eno=input("Enter eno : ")
self.ename=input("Enter ename : ")

def getEmployee(self):
1|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
print("Eno is : ",self.eno)
print("Ename is : ",self.ename)

class Doj:
def setDoj(self): #instance mtd of class Doj
print("Enter Doj ")
self.dd=input("Enter DD : ")
self.mm=input("Enter MM : ")
self.yy=input("Enter YY :")

def getDoj(self):
print("Doj is : {}-{}-{}".format(self.dd,self.mm,self.yy))

#CallingThem
e=Employee()
e.setEmployee()
d=e.Doj()
d.setDoj()

print("- "*30)
e.getEmployee()
d.getDoj()

2|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python

Example 2:

class Student:
def setStudent(self):
self.sno=input("Enter sno : ")
self.sname=input("Enter sname : ")

self.d=self.Dob()
self.d.setDob()

self.m=self.Marks()
self.m.setMarks()

def getStudent(self):
print("- "*30)
print("Sno is : ",self.sno)
print("Sname is : ",self.sname)

3|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
self.d.getDob( )
self.m.getMarks( )

class Dob:
def setDob(self): #instance mtd
print("Enter DOB : ")
self.dd=input("Enter DD : ")
self.mm=input("Enter MM : ")
self.yy=input("Enter YY : ")

def getDob(self):
print("Dob of Student : {}-{}-{}".format(self.dd,self.mm,self.yy))

class Marks:
def setMarks(self):
self.s1=input("Enter Sub1 Marks : ")
self.s2=input("Enter Sub2 Marks : ")
self.s3=input("Enter Sub3 Marks : ")

def getMarks(self):
print("- "*20)
print("Marks are ")
print("Sub1 Marks : ",self.s1)
print("Sub2 Marks : ",self.s2)
print("Sub3 Marks : ",self.s3)

4|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
#Calling
s=Student()
s.setStudent()
s.getStudent()

5|Page

You might also like