0% found this document useful (0 votes)
5 views17 pages

DAP Lab Program - 3 (With Class and Objects Basics)

The document provides an overview of object-oriented programming concepts in Python, including classes, objects, inheritance, and constructors. It includes examples demonstrating how to declare classes, create object methods, and implement inheritance with subclasses. Additionally, it features a mini-project showcasing encapsulation, overloading, and overriding through a Student and PGStudent class implementation.

Uploaded by

srinivas.jaya666
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
5 views17 pages

DAP Lab Program - 3 (With Class and Objects Basics)

The document provides an overview of object-oriented programming concepts in Python, including classes, objects, inheritance, and constructors. It includes examples demonstrating how to declare classes, create object methods, and implement inheritance with subclasses. Additionally, it features a mini-project showcasing encapsulation, overloading, and overriding through a Student and PGStudent class implementation.

Uploaded by

srinivas.jaya666
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 17

Data Analytics Lab

with Mini - Project


22MCAL36
Classes and Objects;
Inheritance
• Declaring a class:

class name:
statements

2
name = value
• Example:
class Point:
x = 0
y = 0
# main
p1 = Point()
p1.x = 2
p1.y = -5

• can be declared directly inside class (as


shown here)
or in constructors (more common)
3
4
Object Methods

def name(self, parameter, ...,


parameter):
statements

• selfmust be the first parameter to any object


method
• represents the "implicit parameter" (this in Java)

• must access the object's fields through the self


reference
class Point:
5 def translate(self, dx, dy):
"Implicit" Parameter (self)

• Java: this, implicit


public void translate(int dx, int dy) {
x += dx; // this.x += dx;
y += dy; // this.y += dy;
}

• Python: self, explicit


def translate(self, dx, dy):
self.x += dx
self.y += dy
6
Constructors
def __init__(self, parameter, ...,
parameter):
statements
• a constructor is a special method with the name
__init__
• Example:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
7
...
Inheritance

class name(superclass):
statements
• Example:
class Point3D(Point): # Point3D extends Point
z = 0
...

• Python also supports multiple inheritance

class name(superclass, ..., superclass):


statements
8
Calling Superclass Methods

• methods: class.method(object, parameters)


• constructors: class.__init__(parameters)

class Point3D(Point):
z = 0
def __init__(self, x, y, z):
Point.__init__(self, x, y)
self.z = z

def translate(self, dx, dy, dz):


Point.translate(self, dx, dy)
self.z += dz

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

def greeting(self, name=None):


if name is not None:
print("Welcome" + name)
else:
print("Welcome")

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

You might also like