Professional Documents
Culture Documents
Basic OOP
Basic OOP
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
else:
Example: Student
class Student:
if age > 0:
self.age = age
else:
self.age = 1
def getAge(self):
return self.age
objStudent = Student()
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
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:
self.setAge(age)
if age > 0:
self.age = age
else:
self.age = 1
def getAge(self):
return self.age
objStudent = Student()
class Student:
self.setAge(age)
if age > 0:
self.age = age
else:
self.age = 1
def getAge(self):
return self.age
objStudent = Student()
printTable()
printTable(3)
printTable(4,5)
printTable(6,7,12)