Object Oriented Programming: in Python
Object Oriented Programming: in Python
in
Python
Team : PERL
Leader : Alexzander B. Sanciangco
Member : John Mari Cad
Rommel Simundo
John Lowel Manila
Joshua Caliwangan
Rodiniel Alcantara
VYBHAVA TECHNOLOGIES
What is an Object..?
6
class student:
“““A class representing a student ”””
def init (self , n, a):
self.full_name = n
self.age = a
def get_age(self): #Method
return self.age
Define class:
Class name, begin with capital letter, by convention
object: class based on (Python built-in type)
Define a method
Like defining a function
Must have a special first parameter, self, which provides way
for a method to refer to object itself
Instantiating Objects with ‘__init__’
11
When you are done with an object, you don’t have to delete
or free it explicitly.
Python has automatic garbage collection.
Python will automatically detect when all of the references to
a piece of memory have gone out of scope. Automatically
frees that memory.
Generally works well, few memory leaks
There’s also no “destructor” method for classes
Syntax for accessing attributes and methods
15
17
Public, Protected and Private Data
18
19
The following interactive sessions shows the behavior of public,
protected and private members:
>>> x = Encapsulation(11,13,17)
>>> x.public
11
>>> x._protected
13
>>> x._protected = 23
>>> x._protected
23
>>> x. private
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Encapsulation' object has no attribute ' private‘
>>>
20
The following table shows the different behavior Public,
Protected and Private Data
Name Notation Behavior
name Public Can be accessed from inside and
outside
21
Inheritance
22
Inheritance is a powerful feature in object oriented programming
It refers to defining a new class with little or no modification to an
existing class.
The new class is called derived (or child) class and the one from which
it inherits is called the base (or parent) class.
Derived class inherits features from the base class, adding new features
to it.
This results into re-usability of code.
Syntax:
class Baseclass(Object):
body_of_base_class
class DerivedClass(BaseClass):
body_of_derived_clas
While designing a inheritance concept, following key pointes keep it
in mind
A sub type never implements less functionality than the super type
Inheritance should never be more than two levels deep
We use inheritance when we want to avoid redundant code.
23
Two built-in functions isinstance() and issubclass() are used
to check inheritances.
Function isinstance() returns True if the object is an instance
of the class or other classes derived from it.
Each and every class in Python inherits from the base
class object.
24
Polymorphism
25
27
Operator Overloading
28