DAP Lab Program - 3 (With Class and Objects Basics)
DAP Lab Program - 3 (With Class and Objects Basics)
class name:
statements
2
name = value
• Example:
class Point:
x = 0
y = 0
# main
p1 = Point()
p1.x = 2
p1.y = -5
class name(superclass):
statements
• Example:
class Point3D(Point): # Point3D extends Point
z = 0
...
class Point3D(Point):
z = 0
def __init__(self, x, y, z):
Point.__init__(self, x, y)
self.z = z
9
#3. Write a python program
using object oriented
programming to demonstrate
Encapsulation,
Overloading/Overriding and
Inheritance.
10
class Student():
# __init__ is known as the constructor
def __init__(self,name,USN):
self.name = name
self.usn = USN
# self.__aadhar = aadhar --can not be accessed in
the subclass
11
def display(self):
print("***Student Object***")
print(self.name)
print(self.usn)
12
# To illustrate Overloading/Overriding
13
# Inheritance - to create a subclass
class PGStudent(Student):
def __init__(self, name, USN, branch):
self.name = name # Super can be used here
self.usn = USN
#specific to PG student
self.branch = branch
14
def display(self):
print("***PG Student Object***")
print(self.name) # parent
print(self.usn) # parent
print(self.branch) # child
15
# creation of a Student (Parent class) class object -
with name and USN
Stu= Student("Virat","1BI21CS012")
Stu.display() # parent class display
Stu.greeting()
# creation of a PGStudent (Sub class) class object
PGStu = PGStudent("Rohit","1BI21MC08","MCA")
PGStu.display() # child class display
PGStu.greeting()
16
• **Student Object***
• Virat 1BI21CS012 Welcome
• ***PG Student Object***
• Rohit 1BI21MC08
• MCA
• Welcome
17