Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

Difference between Procedure Oriented and Object Oriented

  Procedure Oriented object oriented


Divided into In POP program is divided In OOP program is divided into
into small parts called parts called objects.
functions.
Importance In POP importance is not givenIn OOP importance is given to
to data but to function as well as
the data rather than procedures
sequence of actions to be done.
or functions because it works as
a real world.
  POP follows TOP Down approach OOP follows Bottom UP
Approach approach
Access Specifier POP does not have any access OOP has access specifier named
specifier. public, private, protected etc.
Data Moving In POP data can move freely In OOP object can move and
from function to function in the communicate with each other
system. through member functions.
Expansion To add new data and function in OOP provides an easy way to add
POP is not so easy new data and function.
Fundamental concepts of OOP in Python

The four major principles of object orientation are:


Encapsulation
Data Abstraction
Inheritance
Polymorphism
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)
Object Attributes and Methods Example

Object Attributes Store the data for that object


• Object Methods
Example (taxi):
•Define the behaviors for the object Driver
OnDuty
•Example (taxi): NumPassengers
- PickUp Location

- DropOff
- GoOnDuty
- GoOffDuty
- GetDriver
- SetDriver
- GetNumPassengers
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


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


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):
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
A Simple Class def: Student

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
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.
Syntax for accessing attributes and methods

>>> f = student(“Python”, 14)

>>> f.full_name # Access attribute “Python”

>>> f.get_age() # Access a method 14


Method Types

 It Possible to define two kinds of methods with in a class that can be called without an
instance
1) static method
2) class method
3) Instance method
 Normally a class method is passed ‘self’ as its first argument. Self is an instance object
 Some times we need to process data associated with instead of instances
 Let us assume, simple function written outside the class, the code is not well associated with
class, can’t be inherited and the name of the method is not localized
 Hence python offers us static and class methods
Static Method

> Simple function with no self argument


> Nested inside class
> Work on class attribute not on instance attributes
> Can be called through both class and instance
> The built in function static method is used to create them
Syntax For Static Method

class MyClass:
def my_static_method():
----rest of the code---

my_static_method=staticmethod (my_static_method)
Sample program For Static Method

class Students(object):
total = 0
def status():
print('\n Total Number of studetns is :', Students.total)
def __init__(self, name):
self.name= name
Students.total+=1

print('Before Creating instance:', Students.total)


student1=Students('Guido')
student2=Students('Van')
student3=Students('Rossum')
Students.status() # Accessing the class attribute through direct class name student1.status() # Accessing the class
attribute through an object
Output For Sample program
Instance Method

#Types of methods
#instance method
class c1:
def fun(self):
print('hello')

o=c1()
o.fun()
Static Method

#static method
class c2:
@staticmethod
def f1():
print('static method')

c2.f1()
Class Method

#class method
class c3:
@classmethod
def f2(cls):
print('class method')
c3.f2()
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. 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' components, this means, that the internal representation
of an object can't be seen from outside of the objects definition.
Public, Protected and Private Data

 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
Public, Protected and Private Data

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
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‘
>>>
The following table shows the different behavior Public, Protected and Private Data
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
Inheritance
single level inheritance and acces Modifiers modifiers

#single level inheritance and access modifiers


class c1:
def __init__(self,a,b,c):
self.a=a#public
self._b=b#protected
self.__c=c#private

class ch(c1):
pass

och=ch(1,2,3)
print(och.a)
print(och._b)
#print(och.__c)#error because c is private
print(och._c1__c)
Multiple Inheritance

 Multiple inheritance is possible in Python.


 A class can be derived from more than one base classes. The syntax for multiple
inheritance is similar to single inheritance.
 Here is an example of multiple inheritance.

•Syntax:
class Base1:
Statements

class Base2:
Statements

class MultiDerived(Base1, Base2):


Statements
Multiple Inheritance
MultiLevel Inheritance

 On the other hand, we can inherit form a derived class.


 This is also called multilevel inheritance.

 Multilevel inheritance can be of any depth in Python.

 An example with corresponding visualization is given below.

•Syntax:
class Base:
pass

class Derived1(Base):
pass

class Derived2(Derived1):
pass
MultiLevel Inheritance

The class Derivedd1 inherits from Base


and Derivedd2 inherits from both Base as
well as Derived1

You might also like