You are on page 1of 4

class

Object
Encapsulation
getter method
Syntax
setter method
Syntax
Example: Student
Constructor
Syntax
Example: Student
Example: Constructor with default value
Understanding the use/purpuse of default values to the parameters
class
conceptual model of an entity
it is the classification
it defines the attributes and behaviors of the entity
attributes
things which defines the entity
behaviors
actions which an entity can perform

Object
physical form/mapping of the class
attributes will be called as the data members
behaviors will be called as the member functions

Encapsulation
enscapulate (hide from outer world) the data
in programming
making your data members private (it will be accessable within the class)
public methods (getter and setter method)

getter method
retrieval/get/fetch/output method
will get the value of only for the specfic single attribute

Syntax

def getAttributeName(self):

return self.attributeName

self
it represents the object itself

setter method
put/provide/input/set
will set the value of only for the specfic single attribute
validation
setter function is just like = operator for the attributes of the class

Syntax

def setAttributeName(self, attributeName):

if validation: #if true

self.attributeName = attributeName #assigning the

else:

self.attributeName = defaultValue # defaultValue will always be


constant value

Example: Student
class Student:

## Define the Student

def setAge(self, age):

if age > 0:

self.age = age

else:

self.age = 1

def getAge(self):

return self.age

objStudent = Student()

objStudent.setAge(int(input("Enter age: ")))

print("Age =", objStudent.getAge())

Constructor
Construct - behvaior
A function which will be triggered at the construction/creation of the object
It is self-triggered
Purpose
initializer
setter methods of all the attributes

Syntax

def __init__(self, ....):

call all the setter methods

Types of constructor
- Default Constructor
- A constructor with no parameters (other than
self in python)
- Parameterized Constructor
- A constructor with one or more parameters

Example: Student

class Student:

## Define the Student

def __init__(self, age):

self.setAge(age)

def setAge(self, age):

if age > 0:

self.age = age

else:

self.age = 1

def getAge(self):

return self.age

objStudent = Student()

objStudent.setAge(int(input("Enter age: ")))

print("Age =", objStudent.getAge())

Example: Constructor with default value

class Student:

## Define the Student

def __init__(self, age = 10):

self.setAge(age)

def setAge(self, age):

if age > 0:

self.age = age

else:

self.age = 1

def getAge(self):

return self.age

objStudent = Student()

print("Age =", objStudent.getAge())

Understanding the use/purpuse of default values


to the parameters

def printTable(number = 2, start = 1, end = 10):

for multiplier in range(start, end+1):

print(number, "x", multiplier, "=", (number*multiplier))

printTable()

printTable(3)

printTable(4,5)

printTable(6,7,12)

You might also like