SlideShare a Scribd company logo
7/26/2014 VYBHAVA TECHNOLOGIES 1
Contents
> Differences Procedure vs Object Oriented Programming
> Features of OOP
> Fundamental Concepts of OOP in Python
> What is Class
> What is Object
> Methods in Classes
Instantiating objects with __init__
self
> Encapsulation
> Data Abstraction
> Public, Protected and Private Data
> Inheritance
> Polymorphism
> Operator Overloading
7/26/2014 VYBHAVA TECHNOLOGIES 2
Difference between Procedure Oriented and
Object Oriented Programming
๏ถProcedural programming creates a step by step program that
guides the application through a sequence of instructions. Each
instruction is executed in order.
๏ถProcedural programming also focuses on the idea that all
algorithms are executed with functions and data that the
programmer has access to and is able to change.
๏ถObject-Oriented programming is much more similar to the way
the real world works; it is analogous to the human brain. Each
program is made up of many entities called objects.
๏ถInstead, a message must be sent requesting the data, just like
people must ask one another for information; we cannot see
inside each otherโ€™s heads.
7/26/2014 VYBHAVA TECHNOLOGIES 3
Featuers of OOP
๏ถAbility to simulate real-world event much more effectively
๏ถCode is reusable thus less code may have to be written
๏ถData becomes active
๏ถBetter able to create GUI (graphical user interface) applications
๏ถProgrammers are able to produce faster, more accurate and better-
written applications
7/26/2014 VYBHAVA TECHNOLOGIES 4
Fundamental concepts of OOP in Python
The four major principles of object orientation are:
๏ถEncapsulation
๏ถData Abstraction
๏ถInheritance
๏ถPolymorphism
7/26/2014 VYBHAVA TECHNOLOGIES 5
What is an Object..?
๏ถ Objects are the basic run-time entities in an object-oriented
system.
๏ถThey may represent a person, a place, a bank account, a table of
data or any item that the program must handle.
๏ถWhen a program is executed the objects interact by sending
messages to one another.
๏ถObjects have two components:
- Data (i.e., attributes)
- Behaviors (i.e., methods)
7/26/2014 VYBHAVA TECHNOLOGIES 6
Object Attributes and Methods Example
Object Attributes Object Methods
Store the data for that object
Example (taxi):
Driver
OnDuty
NumPassengers
Location
Define the behaviors for the
object
Example (taxi):
- PickUp
- DropOff
- GoOnDuty
- GoOffDuty
- GetDriver
- SetDriver
- GetNumPassengers
7/26/2014 VYBHAVA TECHNOLOGIES 7
What is a Class..?
๏ถA class is a special data type which defines how to build a certain
kind of object.
๏ถThe class also stores some data items that are shared by all the
instances of this class
๏ถ Instances are objects that are created which follow the definition
given inside of the class
๏ถPython doesnโ€™t use separate class interface definitions as in some
languages
๏ถYou just define the class and then use it
7/26/2014 VYBHAVA TECHNOLOGIES 8
Methods in Classes
๏ถDefine a method in a class by including function definitions
within the scope of the class block
๏ถThere must be a special first argument self in all of method
definitions which gets bound to the calling instance
๏ถThere is usually a special method called __init__ in most
classes
7/26/2014 VYBHAVA TECHNOLOGIES 9
A Simple Class def: Student
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
7/26/2014 VYBHAVA TECHNOLOGIES 10
Instantiating Objects with โ€˜__init__โ€™
๏ถ __init__ is the default constructor
๏ถ__init__ serves as a constructor for the class. Usually
does some initialization work
๏ถAn __init__ method can take any number of
arguments
๏ถHowever, the first argument self in the definition of
__init__ is special
7/26/2014 VYBHAVA TECHNOLOGIES 11
Self
๏ถThe first argument of every method is a reference to the current
instance of the class
๏ถBy convention, we name this argument self
๏ถIn __init__, self refers to the object currently being created; so, in
other class methods, it refers to the instance whose method was
called
๏ถSimilar to the keyword this in Java or C++
๏ถBut Python uses self more often than Java uses this
๏ถYou do not give a value for this parameter(self) when you call the
method, Python will provide it.
Continueโ€ฆ
7/26/2014 VYBHAVA TECHNOLOGIES 12
โ€ฆContinue
๏ถAlthough you must specify self explicitly when defining the
method, you donโ€™t include it when calling the method.
๏ถPython passes it for you automatically
Defining a method: Calling a method:
(this code inside a class definition.)
def get_age(self, num): >>> x.get_age(23)
self.age = num
7/26/2014 VYBHAVA TECHNOLOGIES 13
Deleting instances: No Need to โ€œfreeโ€
7/26/2014 VYBHAVA TECHNOLOGIES 14
๏ถ 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
>>> f = student(โ€œPythonโ€, 14)
>>> f.full_name # Access attribute
โ€œPythonโ€
>>> f.get_age() # Access a method
14
7/26/2014 VYBHAVA TECHNOLOGIES 15
Encapsulation
๏ถImportant advantage of OOP consists in the encapsulation
of data. We can say that object-oriented programming
relies heavily on encapsulation.
๏ถThe terms encapsulation and abstraction (also data hiding)
are often used as synonyms. They are nearly synonymous,
i.e. abstraction is achieved though encapsulation.
๏ถData hiding and encapsulation are the same concept, so it's
correct to use them as synonyms
๏ถGenerally speaking encapsulation is the mechanism for
restricting the access to some of an objects's components,
this means, that the internal representation of an object
can't be seen from outside of the objects definition.
7/26/2014 VYBHAVA TECHNOLOGIES 16
๏ถ Access to this data is typically only achieved through
special methods: Getters and Setters
๏ถBy using solely get() and set() methods, we can make sure
that the internal data cannot be accidentally set into an
inconsistent or invalid state.
๏ถC++, Java, and C# rely on the public, private,
and protected keywords in order to implement variable
scoping and encapsulation
๏ถ It's nearly always possible to circumvent this protection
mechanism
7/26/2014 VYBHAVA TECHNOLOGIES 17
Public, Protected and Private Data
7/26/2014 VYBHAVA TECHNOLOGIES 18
๏ถ If an identifier doesn't start with an underscore character "_" it
can be accessed from outside, i.e. the value can be read and
changed
๏ถ Data can be protected by making members private or protected.
Instance variable names starting with two underscore characters
cannot be accessed from outside of the class.
๏ถ At least not directly, but they can be accessed through private
name mangling.
๏ถ That means, private data __A can be accessed by the following
name construct: instance_name._classname__A
๏ถ If an identifier is only preceded by one underscore character, it
is a protected member.
๏ถ Protected members can be accessed like public members from
outside of class
Example:
class Encapsulation(object):
def __init__(self, a, b, c):
self.public = a
self._protected = b
self.__private = c
7/26/2014 VYBHAVA TECHNOLOGIES 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โ€˜
>>>
7/26/2014 VYBHAVA TECHNOLOGIES 20
The following table shows the different behavior Public,
Protected and Private Data
7/26/2014 VYBHAVA TECHNOLOGIES 21
Name Notation Behavior
name Public Can be accessed from inside and
outside
_name Protected Like a public member, but they
shouldn't be directly accessed from
outside
__name Private Can't be seen and accessed from
outside
Inheritance
๏ถ 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
7/26/2014 VYBHAVA TECHNOLOGIES 22
7/26/2014 VYBHAVA TECHNOLOGIES 23
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.
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.
7/26/2014 VYBHAVA TECHNOLOGIES 24
Polymorphism
๏ถPolymorphism in Latin word which made up of โ€˜ployโ€™
means many and โ€˜morphsโ€™ means forms
๏ถFrom the Greek , Polymorphism means many(poly)
shapes (morph)
๏ถ This is something similar to a word having several different
meanings depending on the context
๏ถ Generally speaking, polymorphism means that a method or
function is able to cope with different types of input.
7/26/2014 VYBHAVA TECHNOLOGIES 25
A simple word โ€˜Cutโ€™ can have different meaning
depending where it is used
7/26/2014 VYBHAVA TECHNOLOGIES 26
Cut
Surgeon: The Surgeon
would begin to make an
incision
Hair Stylist: The Hair
Stylist would begin to cut
someoneโ€™s hair
Actor: The actor would
abruptly stop acting out the
current scene, awaiting
directional guidance
If any body says โ€œCutโ€ to these people
In OOP , Polymorphism is the characteristic of being able to
assign a different meaning to a particular symbol or operator in
different contexts specifically to allow an entity such as a
variable, a function or an object to have more than one form.
There are two kinds of Polymorphism
Overloading :
Two or more methods with different signatures
Overriding:
Replacing an inherited method with another having the same
signature
7/26/2014 VYBHAVA TECHNOLOGIES 27
๏ถPython operators work for built-in classes.
๏ถBut same operator behaves differently with different types. For
example, the + operator will, perform arithmetic addition on
two numbers, merge two lists and concatenate two strings. This
feature in Python, that allows same operator to have different
meaning according to the context is called operator overloading
๏ถOne final thing to mention about operator overloading is that
you can make your custom methods do whatever you want.
However, common practice is to follow the structure of the
built-in methods.
7/26/2014 VYBHAVA TECHNOLOGIES 28
Operator Overloading
Explanation for Operator Overloading Sample Program
What actually happens is that, when you do p1 - p2, Python will
call p1.__sub__(p2) which in turn is Point.__sub__(p1,p2).
Similarly, we can overload other operators as well. The special
function that we need to implement is tabulated below.
7/26/2014 VYBHAVA TECHNOLOGIES 29
Operator Expression Internally
Addition p1 + p2 p1.__add__(p2)
Subtraction p1 โ€“ p2 p1.__sub__(p2)
Multiplication p1 * p2 p1.__mul__(p2)
Power p1 ** p2 p1.__pow__(p2)
Division p1 / p2 p1.__truediv__(p2)
Ad

More Related Content

What's hot (20)

Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
Emertxe Information Technologies Pvt Ltd
ย 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
Praveen M Jigajinni
ย 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
ย 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
ย 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
Praveen M Jigajinni
ย 
Python Libraries and Modules
Python Libraries and ModulesPython Libraries and Modules
Python Libraries and Modules
RaginiJain21
ย 
Python OOPs
Python OOPsPython OOPs
Python OOPs
Binay Kumar Ray
ย 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
Vinod Kumar
ย 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
ย 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
Jussi Pohjolainen
ย 
Database connectivity in python
Database connectivity in pythonDatabase connectivity in python
Database connectivity in python
baabtra.com - No. 1 supplier of quality freshers
ย 
Operators in python
Operators in pythonOperators in python
Operators in python
Prabhakaran V M
ย 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
Boopathi K
ย 
Java package
Java packageJava package
Java package
CS_GDRCST
ย 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
ย 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
ย 
Python Modules
Python ModulesPython Modules
Python Modules
Nitin Reddy Katkam
ย 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
ย 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
ย 
Java constructors
Java constructorsJava constructors
Java constructors
QUONTRASOLUTIONS
ย 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
ย 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
ย 
Chapter 03 python libraries
Chapter 03 python librariesChapter 03 python libraries
Chapter 03 python libraries
Praveen M Jigajinni
ย 
Python Libraries and Modules
Python Libraries and ModulesPython Libraries and Modules
Python Libraries and Modules
RaginiJain21
ย 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
Vinod Kumar
ย 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
ย 
Operators in python
Operators in pythonOperators in python
Operators in python
Prabhakaran V M
ย 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
Boopathi K
ย 
Java package
Java packageJava package
Java package
CS_GDRCST
ย 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
ย 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
ย 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
ย 
Java constructors
Java constructorsJava constructors
Java constructors
QUONTRASOLUTIONS
ย 

Viewers also liked (14)

Python 2 vs. Python 3
Python 2 vs. Python 3Python 2 vs. Python 3
Python 2 vs. Python 3
Pablo Enfedaque
ย 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
Sujith Kumar
ย 
Python Tricks That You Can't Live Without
Python Tricks That You Can't Live WithoutPython Tricks That You Can't Live Without
Python Tricks That You Can't Live Without
Audrey Roy
ย 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
amiable_indian
ย 
Python็š„50้“้™ฐๅฝฑ
Python็š„50้“้™ฐๅฝฑPython็š„50้“้™ฐๅฝฑ
Python็š„50้“้™ฐๅฝฑ
Tim (ๆ–‡ๆ˜Œ)
ย 
้€ฃๆทกๆฐด้˜ฟๅฌค้ƒฝ่ฝๅพ—ๆ‡‚็š„ ๆฉŸๅ™จๅญธ็ฟ’ๅ…ฅ้–€ scikit-learn
้€ฃๆทกๆฐด้˜ฟๅฌค้ƒฝ่ฝๅพ—ๆ‡‚็š„ๆฉŸๅ™จๅญธ็ฟ’ๅ…ฅ้–€ scikit-learn ้€ฃๆทกๆฐด้˜ฟๅฌค้ƒฝ่ฝๅพ—ๆ‡‚็š„ๆฉŸๅ™จๅญธ็ฟ’ๅ…ฅ้–€ scikit-learn
้€ฃๆทกๆฐด้˜ฟๅฌค้ƒฝ่ฝๅพ—ๆ‡‚็š„ ๆฉŸๅ™จๅญธ็ฟ’ๅ…ฅ้–€ scikit-learn
Cicilia Lee
ย 
ๅธธ็”จๅ…งๅปบๆจก็ต„
ๅธธ็”จๅ…งๅปบๆจก็ต„ๅธธ็”จๅ…งๅปบๆจก็ต„
ๅธธ็”จๅ…งๅปบๆจก็ต„
Justin Lin
ย 
ๅž‹ๆ…‹่ˆ‡้‹็ฎ—ๅญ
ๅž‹ๆ…‹่ˆ‡้‹็ฎ—ๅญๅž‹ๆ…‹่ˆ‡้‹็ฎ—ๅญ
ๅž‹ๆ…‹่ˆ‡้‹็ฎ—ๅญ
Justin Lin
ย 
้€ฒ้šŽไธป้กŒ
้€ฒ้šŽไธป้กŒ้€ฒ้šŽไธป้กŒ
้€ฒ้šŽไธป้กŒ
Justin Lin
ย 
Python ่ตทๆญฅ่ตฐ
Python ่ตทๆญฅ่ตฐPython ่ตทๆญฅ่ตฐ
Python ่ตทๆญฅ่ตฐ
Justin Lin
ย 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
Matt Harrison
ย 
[็ณปๅˆ—ๆดปๅ‹•] Python ็จ‹ๅผ่ชž่จ€่ตทๆญฅ่ตฐ
[็ณปๅˆ—ๆดปๅ‹•] Python ็จ‹ๅผ่ชž่จ€่ตทๆญฅ่ตฐ[็ณปๅˆ—ๆดปๅ‹•] Python ็จ‹ๅผ่ชž่จ€่ตทๆญฅ่ตฐ
[็ณปๅˆ—ๆดปๅ‹•] Python ็จ‹ๅผ่ชž่จ€่ตทๆญฅ่ตฐ
ๅฐ็ฃ่ณ‡ๆ–™็ง‘ๅญธๅนดๆœƒ
ย 
[็ณปๅˆ—ๆดปๅ‹•] Python็ˆฌ่Ÿฒๅฏฆๆˆฐ
[็ณปๅˆ—ๆดปๅ‹•] Python็ˆฌ่Ÿฒๅฏฆๆˆฐ[็ณปๅˆ—ๆดปๅ‹•] Python็ˆฌ่Ÿฒๅฏฆๆˆฐ
[็ณปๅˆ—ๆดปๅ‹•] Python็ˆฌ่Ÿฒๅฏฆๆˆฐ
ๅฐ็ฃ่ณ‡ๆ–™็ง‘ๅญธๅนดๆœƒ
ย 
[็ณปๅˆ—ๆดปๅ‹•] ็„กๆ‰€ไธๅœจ็š„่‡ช็„ถ่ชž่จ€่™•็†โ€”ๅŸบ็คŽๆฆ‚ๅฟตใ€ๆŠ€่ก“่ˆ‡ๅทฅๅ…ทไป‹็ดน
[็ณปๅˆ—ๆดปๅ‹•] ็„กๆ‰€ไธๅœจ็š„่‡ช็„ถ่ชž่จ€่™•็†โ€”ๅŸบ็คŽๆฆ‚ๅฟตใ€ๆŠ€่ก“่ˆ‡ๅทฅๅ…ทไป‹็ดน[็ณปๅˆ—ๆดปๅ‹•] ็„กๆ‰€ไธๅœจ็š„่‡ช็„ถ่ชž่จ€่™•็†โ€”ๅŸบ็คŽๆฆ‚ๅฟตใ€ๆŠ€่ก“่ˆ‡ๅทฅๅ…ทไป‹็ดน
[็ณปๅˆ—ๆดปๅ‹•] ็„กๆ‰€ไธๅœจ็š„่‡ช็„ถ่ชž่จ€่™•็†โ€”ๅŸบ็คŽๆฆ‚ๅฟตใ€ๆŠ€่ก“่ˆ‡ๅทฅๅ…ทไป‹็ดน
ๅฐ็ฃ่ณ‡ๆ–™็ง‘ๅญธๅนดๆœƒ
ย 
Python 2 vs. Python 3
Python 2 vs. Python 3Python 2 vs. Python 3
Python 2 vs. Python 3
Pablo Enfedaque
ย 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
Sujith Kumar
ย 
Python Tricks That You Can't Live Without
Python Tricks That You Can't Live WithoutPython Tricks That You Can't Live Without
Python Tricks That You Can't Live Without
Audrey Roy
ย 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
amiable_indian
ย 
Python็š„50้“้™ฐๅฝฑ
Python็š„50้“้™ฐๅฝฑPython็š„50้“้™ฐๅฝฑ
Python็š„50้“้™ฐๅฝฑ
Tim (ๆ–‡ๆ˜Œ)
ย 
้€ฃๆทกๆฐด้˜ฟๅฌค้ƒฝ่ฝๅพ—ๆ‡‚็š„ ๆฉŸๅ™จๅญธ็ฟ’ๅ…ฅ้–€ scikit-learn
้€ฃๆทกๆฐด้˜ฟๅฌค้ƒฝ่ฝๅพ—ๆ‡‚็š„ๆฉŸๅ™จๅญธ็ฟ’ๅ…ฅ้–€ scikit-learn ้€ฃๆทกๆฐด้˜ฟๅฌค้ƒฝ่ฝๅพ—ๆ‡‚็š„ๆฉŸๅ™จๅญธ็ฟ’ๅ…ฅ้–€ scikit-learn
้€ฃๆทกๆฐด้˜ฟๅฌค้ƒฝ่ฝๅพ—ๆ‡‚็š„ ๆฉŸๅ™จๅญธ็ฟ’ๅ…ฅ้–€ scikit-learn
Cicilia Lee
ย 
ๅธธ็”จๅ…งๅปบๆจก็ต„
ๅธธ็”จๅ…งๅปบๆจก็ต„ๅธธ็”จๅ…งๅปบๆจก็ต„
ๅธธ็”จๅ…งๅปบๆจก็ต„
Justin Lin
ย 
ๅž‹ๆ…‹่ˆ‡้‹็ฎ—ๅญ
ๅž‹ๆ…‹่ˆ‡้‹็ฎ—ๅญๅž‹ๆ…‹่ˆ‡้‹็ฎ—ๅญ
ๅž‹ๆ…‹่ˆ‡้‹็ฎ—ๅญ
Justin Lin
ย 
้€ฒ้šŽไธป้กŒ
้€ฒ้šŽไธป้กŒ้€ฒ้šŽไธป้กŒ
้€ฒ้šŽไธป้กŒ
Justin Lin
ย 
Python ่ตทๆญฅ่ตฐ
Python ่ตทๆญฅ่ตฐPython ่ตทๆญฅ่ตฐ
Python ่ตทๆญฅ่ตฐ
Justin Lin
ย 
Learn 90% of Python in 90 Minutes
Learn 90% of Python in 90 MinutesLearn 90% of Python in 90 Minutes
Learn 90% of Python in 90 Minutes
Matt Harrison
ย 
[็ณปๅˆ—ๆดปๅ‹•] ็„กๆ‰€ไธๅœจ็š„่‡ช็„ถ่ชž่จ€่™•็†โ€”ๅŸบ็คŽๆฆ‚ๅฟตใ€ๆŠ€่ก“่ˆ‡ๅทฅๅ…ทไป‹็ดน
[็ณปๅˆ—ๆดปๅ‹•] ็„กๆ‰€ไธๅœจ็š„่‡ช็„ถ่ชž่จ€่™•็†โ€”ๅŸบ็คŽๆฆ‚ๅฟตใ€ๆŠ€่ก“่ˆ‡ๅทฅๅ…ทไป‹็ดน[็ณปๅˆ—ๆดปๅ‹•] ็„กๆ‰€ไธๅœจ็š„่‡ช็„ถ่ชž่จ€่™•็†โ€”ๅŸบ็คŽๆฆ‚ๅฟตใ€ๆŠ€่ก“่ˆ‡ๅทฅๅ…ทไป‹็ดน
[็ณปๅˆ—ๆดปๅ‹•] ็„กๆ‰€ไธๅœจ็š„่‡ช็„ถ่ชž่จ€่™•็†โ€”ๅŸบ็คŽๆฆ‚ๅฟตใ€ๆŠ€่ก“่ˆ‡ๅทฅๅ…ทไป‹็ดน
ๅฐ็ฃ่ณ‡ๆ–™็ง‘ๅญธๅนดๆœƒ
ย 
Ad

Similar to Basics of Object Oriented Programming in Python (20)

oopinpyhtonnew-140722060241-phpapp01.pptx
oopinpyhtonnew-140722060241-phpapp01.pptxoopinpyhtonnew-140722060241-phpapp01.pptx
oopinpyhtonnew-140722060241-phpapp01.pptx
smartashammari
ย 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
Sujith Kumar
ย 
Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
Sudip Simkhada
ย 
OOP interview questions & answers.
OOP interview questions & answers.OOP interview questions & answers.
OOP interview questions & answers.
Questpond
ย 
OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptx
mrxyz19
ย 
Object Oriented Javascript part2
Object Oriented Javascript part2Object Oriented Javascript part2
Object Oriented Javascript part2
Usman Mehmood
ย 
Selenium Training .pptx
Selenium Training .pptxSelenium Training .pptx
Selenium Training .pptx
SajidTk2
ย 
Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1
Synapseindiappsdevelopment
ย 
Object-Oriented Programming in Java.pdf
Object-Oriented Programming in Java.pdfObject-Oriented Programming in Java.pdf
Object-Oriented Programming in Java.pdf
Bharath Choudhary
ย 
My c++
My c++My c++
My c++
snathick
ย 
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
AnkurSingh340457
ย 
OOPs Interview Questions PDF By ScholarHat
OOPs Interview Questions PDF By ScholarHatOOPs Interview Questions PDF By ScholarHat
OOPs Interview Questions PDF By ScholarHat
Scholarhat
ย 
python.pptx
python.pptxpython.pptx
python.pptx
Dhanushrajucm
ย 
Oops concepts
Oops conceptsOops concepts
Oops concepts
ACCESS Health Digital
ย 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
SadiqullahGhani1
ย 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
Ajay Chimmani
ย 
Object oriented basics
Object oriented basicsObject oriented basics
Object oriented basics
vamshimahi
ย 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Sagar Verma
ย 
Oops
OopsOops
Oops
Sankar Balasubramanian
ย 
Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHP
Rick Ogden
ย 
oopinpyhtonnew-140722060241-phpapp01.pptx
oopinpyhtonnew-140722060241-phpapp01.pptxoopinpyhtonnew-140722060241-phpapp01.pptx
oopinpyhtonnew-140722060241-phpapp01.pptx
smartashammari
ย 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
Sujith Kumar
ย 
OOP interview questions & answers.
OOP interview questions & answers.OOP interview questions & answers.
OOP interview questions & answers.
Questpond
ย 
OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptx
mrxyz19
ย 
Object Oriented Javascript part2
Object Oriented Javascript part2Object Oriented Javascript part2
Object Oriented Javascript part2
Usman Mehmood
ย 
Selenium Training .pptx
Selenium Training .pptxSelenium Training .pptx
Selenium Training .pptx
SajidTk2
ย 
Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1Synapseindia strcture of dotnet development part 1
Synapseindia strcture of dotnet development part 1
Synapseindiappsdevelopment
ย 
Object-Oriented Programming in Java.pdf
Object-Oriented Programming in Java.pdfObject-Oriented Programming in Java.pdf
Object-Oriented Programming in Java.pdf
Bharath Choudhary
ย 
My c++
My c++My c++
My c++
snathick
ย 
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
FAL(2022-23)_CSE0206_ETH_AP2022232000455_Reference_Material_I_16-Aug-2022_Mod...
AnkurSingh340457
ย 
OOPs Interview Questions PDF By ScholarHat
OOPs Interview Questions PDF By ScholarHatOOPs Interview Questions PDF By ScholarHat
OOPs Interview Questions PDF By ScholarHat
Scholarhat
ย 
python.pptx
python.pptxpython.pptx
python.pptx
Dhanushrajucm
ย 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
SadiqullahGhani1
ย 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
Ajay Chimmani
ย 
Object oriented basics
Object oriented basicsObject oriented basics
Object oriented basics
vamshimahi
ย 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Sagar Verma
ย 
Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHP
Rick Ogden
ย 
Ad

Recently uploaded (20)

Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
ย 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
ย 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
ย 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
ย 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
ย 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
ย 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
ย 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
ย 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
ย 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
ย 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
ย 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
ย 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
ย 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
ย 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
ย 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
ย 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
ย 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
ย 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
ย 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
ย 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
ย 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
ย 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
ย 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
ย 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
ย 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
ย 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
ย 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
ย 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
ย 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
ย 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
ย 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
ย 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
ย 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
ย 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
ย 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
ย 
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven InsightsAndrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell: Transforming Business Strategy Through Data-Driven Insights
Andrew Marnell
ย 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
ย 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
ย 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
ย 

Basics of Object Oriented Programming in Python

  • 2. Contents > Differences Procedure vs Object Oriented Programming > Features of OOP > Fundamental Concepts of OOP in Python > What is Class > What is Object > Methods in Classes Instantiating objects with __init__ self > Encapsulation > Data Abstraction > Public, Protected and Private Data > Inheritance > Polymorphism > Operator Overloading 7/26/2014 VYBHAVA TECHNOLOGIES 2
  • 3. Difference between Procedure Oriented and Object Oriented Programming ๏ถProcedural programming creates a step by step program that guides the application through a sequence of instructions. Each instruction is executed in order. ๏ถProcedural programming also focuses on the idea that all algorithms are executed with functions and data that the programmer has access to and is able to change. ๏ถObject-Oriented programming is much more similar to the way the real world works; it is analogous to the human brain. Each program is made up of many entities called objects. ๏ถInstead, a message must be sent requesting the data, just like people must ask one another for information; we cannot see inside each otherโ€™s heads. 7/26/2014 VYBHAVA TECHNOLOGIES 3
  • 4. Featuers of OOP ๏ถAbility to simulate real-world event much more effectively ๏ถCode is reusable thus less code may have to be written ๏ถData becomes active ๏ถBetter able to create GUI (graphical user interface) applications ๏ถProgrammers are able to produce faster, more accurate and better- written applications 7/26/2014 VYBHAVA TECHNOLOGIES 4
  • 5. Fundamental concepts of OOP in Python The four major principles of object orientation are: ๏ถEncapsulation ๏ถData Abstraction ๏ถInheritance ๏ถPolymorphism 7/26/2014 VYBHAVA TECHNOLOGIES 5
  • 6. What is an Object..? ๏ถ Objects are the basic run-time entities in an object-oriented system. ๏ถThey may represent a person, a place, a bank account, a table of data or any item that the program must handle. ๏ถWhen a program is executed the objects interact by sending messages to one another. ๏ถObjects have two components: - Data (i.e., attributes) - Behaviors (i.e., methods) 7/26/2014 VYBHAVA TECHNOLOGIES 6
  • 7. Object Attributes and Methods Example Object Attributes Object Methods Store the data for that object Example (taxi): Driver OnDuty NumPassengers Location Define the behaviors for the object Example (taxi): - PickUp - DropOff - GoOnDuty - GoOffDuty - GetDriver - SetDriver - GetNumPassengers 7/26/2014 VYBHAVA TECHNOLOGIES 7
  • 8. What is a Class..? ๏ถA class is a special data type which defines how to build a certain kind of object. ๏ถThe class also stores some data items that are shared by all the instances of this class ๏ถ Instances are objects that are created which follow the definition given inside of the class ๏ถPython doesnโ€™t use separate class interface definitions as in some languages ๏ถYou just define the class and then use it 7/26/2014 VYBHAVA TECHNOLOGIES 8
  • 9. Methods in Classes ๏ถDefine a method in a class by including function definitions within the scope of the class block ๏ถThere must be a special first argument self in all of method definitions which gets bound to the calling instance ๏ถThere is usually a special method called __init__ in most classes 7/26/2014 VYBHAVA TECHNOLOGIES 9
  • 10. A Simple Class def: Student 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 7/26/2014 VYBHAVA TECHNOLOGIES 10
  • 11. Instantiating Objects with โ€˜__init__โ€™ ๏ถ __init__ is the default constructor ๏ถ__init__ serves as a constructor for the class. Usually does some initialization work ๏ถAn __init__ method can take any number of arguments ๏ถHowever, the first argument self in the definition of __init__ is special 7/26/2014 VYBHAVA TECHNOLOGIES 11
  • 12. Self ๏ถThe first argument of every method is a reference to the current instance of the class ๏ถBy convention, we name this argument self ๏ถIn __init__, self refers to the object currently being created; so, in other class methods, it refers to the instance whose method was called ๏ถSimilar to the keyword this in Java or C++ ๏ถBut Python uses self more often than Java uses this ๏ถYou do not give a value for this parameter(self) when you call the method, Python will provide it. Continueโ€ฆ 7/26/2014 VYBHAVA TECHNOLOGIES 12
  • 13. โ€ฆContinue ๏ถAlthough you must specify self explicitly when defining the method, you donโ€™t include it when calling the method. ๏ถPython passes it for you automatically Defining a method: Calling a method: (this code inside a class definition.) def get_age(self, num): >>> x.get_age(23) self.age = num 7/26/2014 VYBHAVA TECHNOLOGIES 13
  • 14. Deleting instances: No Need to โ€œfreeโ€ 7/26/2014 VYBHAVA TECHNOLOGIES 14 ๏ถ 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
  • 15. Syntax for accessing attributes and methods >>> f = student(โ€œPythonโ€, 14) >>> f.full_name # Access attribute โ€œPythonโ€ >>> f.get_age() # Access a method 14 7/26/2014 VYBHAVA TECHNOLOGIES 15
  • 16. Encapsulation ๏ถImportant advantage of OOP consists in the encapsulation of data. We can say that object-oriented programming relies heavily on encapsulation. ๏ถThe terms encapsulation and abstraction (also data hiding) are often used as synonyms. They are nearly synonymous, i.e. abstraction is achieved though encapsulation. ๏ถData hiding and encapsulation are the same concept, so it's correct to use them as synonyms ๏ถGenerally speaking encapsulation is the mechanism for restricting the access to some of an objects's components, this means, that the internal representation of an object can't be seen from outside of the objects definition. 7/26/2014 VYBHAVA TECHNOLOGIES 16
  • 17. ๏ถ Access to this data is typically only achieved through special methods: Getters and Setters ๏ถBy using solely get() and set() methods, we can make sure that the internal data cannot be accidentally set into an inconsistent or invalid state. ๏ถC++, Java, and C# rely on the public, private, and protected keywords in order to implement variable scoping and encapsulation ๏ถ It's nearly always possible to circumvent this protection mechanism 7/26/2014 VYBHAVA TECHNOLOGIES 17
  • 18. Public, Protected and Private Data 7/26/2014 VYBHAVA TECHNOLOGIES 18 ๏ถ If an identifier doesn't start with an underscore character "_" it can be accessed from outside, i.e. the value can be read and changed ๏ถ Data can be protected by making members private or protected. Instance variable names starting with two underscore characters cannot be accessed from outside of the class. ๏ถ At least not directly, but they can be accessed through private name mangling. ๏ถ That means, private data __A can be accessed by the following name construct: instance_name._classname__A
  • 19. ๏ถ If an identifier is only preceded by one underscore character, it is a protected member. ๏ถ Protected members can be accessed like public members from outside of class Example: class Encapsulation(object): def __init__(self, a, b, c): self.public = a self._protected = b self.__private = c 7/26/2014 VYBHAVA TECHNOLOGIES 19
  • 20. 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โ€˜ >>> 7/26/2014 VYBHAVA TECHNOLOGIES 20
  • 21. The following table shows the different behavior Public, Protected and Private Data 7/26/2014 VYBHAVA TECHNOLOGIES 21 Name Notation Behavior name Public Can be accessed from inside and outside _name Protected Like a public member, but they shouldn't be directly accessed from outside __name Private Can't be seen and accessed from outside
  • 22. Inheritance ๏ถ 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 7/26/2014 VYBHAVA TECHNOLOGIES 22
  • 23. 7/26/2014 VYBHAVA TECHNOLOGIES 23 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.
  • 24. 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. 7/26/2014 VYBHAVA TECHNOLOGIES 24
  • 25. Polymorphism ๏ถPolymorphism in Latin word which made up of โ€˜ployโ€™ means many and โ€˜morphsโ€™ means forms ๏ถFrom the Greek , Polymorphism means many(poly) shapes (morph) ๏ถ This is something similar to a word having several different meanings depending on the context ๏ถ Generally speaking, polymorphism means that a method or function is able to cope with different types of input. 7/26/2014 VYBHAVA TECHNOLOGIES 25
  • 26. A simple word โ€˜Cutโ€™ can have different meaning depending where it is used 7/26/2014 VYBHAVA TECHNOLOGIES 26 Cut Surgeon: The Surgeon would begin to make an incision Hair Stylist: The Hair Stylist would begin to cut someoneโ€™s hair Actor: The actor would abruptly stop acting out the current scene, awaiting directional guidance If any body says โ€œCutโ€ to these people
  • 27. In OOP , Polymorphism is the characteristic of being able to assign a different meaning to a particular symbol or operator in different contexts specifically to allow an entity such as a variable, a function or an object to have more than one form. There are two kinds of Polymorphism Overloading : Two or more methods with different signatures Overriding: Replacing an inherited method with another having the same signature 7/26/2014 VYBHAVA TECHNOLOGIES 27
  • 28. ๏ถPython operators work for built-in classes. ๏ถBut same operator behaves differently with different types. For example, the + operator will, perform arithmetic addition on two numbers, merge two lists and concatenate two strings. This feature in Python, that allows same operator to have different meaning according to the context is called operator overloading ๏ถOne final thing to mention about operator overloading is that you can make your custom methods do whatever you want. However, common practice is to follow the structure of the built-in methods. 7/26/2014 VYBHAVA TECHNOLOGIES 28 Operator Overloading
  • 29. Explanation for Operator Overloading Sample Program What actually happens is that, when you do p1 - p2, Python will call p1.__sub__(p2) which in turn is Point.__sub__(p1,p2). Similarly, we can overload other operators as well. The special function that we need to implement is tabulated below. 7/26/2014 VYBHAVA TECHNOLOGIES 29 Operator Expression Internally Addition p1 + p2 p1.__add__(p2) Subtraction p1 โ€“ p2 p1.__sub__(p2) Multiplication p1 * p2 p1.__mul__(p2) Power p1 ** p2 p1.__pow__(p2) Division p1 / p2 p1.__truediv__(p2)

Editor's Notes

  • #22: Need improve difference between protected vs public/private