Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views32 pages

OOP - Python 1

This chapter is helpefull for student who are seeking to learn python oop and all the topic are disscused

Uploaded by

fall3210060
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views32 pages

OOP - Python 1

This chapter is helpefull for student who are seeking to learn python oop and all the topic are disscused

Uploaded by

fall3210060
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Object oriented

programming
OOP:

 Object oriented programming (Oop) is a programming technique


in which programs are written on the basis of object.

 An object is a collection of data and functions. And object may


represent a person, thing or place in real world.

 Objects are Real world Entities.

 And object oriented programming is a powerful technique to


develop software.

 Example of Object oriented programming are as

 C++,Java,Smalltalk,CLOS,Eiffel,Python.
Features of Object Oriented Programming

I. Classes

II. Object

III. Constructors

IV. Inheritance

V. Polymorphism
Features of OOP

 Object :OOP provide the facility of programming based on


objects. An object is an entity that consist of data and
function.

 Classes: classes are the design for creating objects. OOP


provides facility to design a class. All properties and functions
of an object are specified in classes.

 Real World Modeling : OOP is based on real-world


modeling .the entities in the have different properties and
working capabilities. Similarly the objects have data and
functions. Data represents the properties and function
represents the working of the objects.
Conti…

 Reusability: OOP allows the programmer to reuse the data


and code. It uses the technique of inheritance to use the code
of exixting class to create new class.

 Encapsulation: OOP allows the programmer to hide the


important data from the user.

 Polymorphism: is the ability of an object to behave in


multiple ways.
Objects
 An object represents an entity in the real world such as a
person , thing or concept etc.
 it is identified by its name. an object consist of the following:
 Properties : these are the characteristics of an object.
 Functions : these are the action that can performed by an
object.

 Examples are
 Physical Objects : vehicle such as Car, Bus, truck etc.
 Element of Computer Enivornment : Windows, Menus,
mouse, keyboard etc
Properties of Object
 Each object has its own properties. These properties can be used to
describe the object. For example the properties of An object Person
can be as follows:
 Person : name, age, weight, Height
 Car : Color, Price, Model, Engine Power
 The set of values of the attribute of a particular object is called its
state. It means that the state of an object can be determined by the
values of its attributes.
 Properties of An object Car follows: Car

Model
Honda Civic
Color
Gray
Price
200000
Function of Object
 The action that can perform by an Object is known as functions or
methods. An object can perform different tasks and actions. For
example the object car can perform the following functions:
 Car : Stop, Accelerate, reverse
 The set of all functions of an objects represents the behavior of the
object . It means that the behavior of an object can be determined by
the list of functions of that object.
 Function of an object Car as Follows:
Car

Start
Stop
Accelerate
Reverse
Classes
 A collection of objects with the same properties and functions is
known as class.
 The class is like a ‘Blueprint’ or ‘Template’ for creating Objects.
 A class is used to define object. It is used as a model for creating
different objects of same type.
 For example : a class person can be used to define the
characteristics and function of a person.
 The person class can be used to create different objects such as
Ali, Usman ,Kamal, Abdullah etc. All objects of person class will
have the same characteristics and functions. However the values
of each object can be different. the values are assigned when an
object is created.
 Each object of a class is known as instance of its class for example
Ali ,Usman and Abdullah are three instances of person Class.
Our first Program in OOP
Conti….
Conti…
Self Parameter

 self parameter is a reference to the current instance of


the class, And is used to access variables that belongs to
the class.
 It allows us to access the attributes and methods of the
object.
Constructors
 It is a special method with in class that gets automatically
called when an object is created.

 All classes have a function called _ _init_ _( ),which is always


executed when the object is being initiated.

 It is used to initializes the attribute of the object.

 It is written with _ _init_ _


Default Constructor : Example 1
Parameterized Constructor :
Example 2
Encapsulation

 Encapsulation in Python is a fundamental concept of


object-oriented programming that involves bundling data
(attributes) and methods (functions) that operate on that
data within a single unit, known as a class.

 It restricts direct access to some of an object's


components, which is a way of preventing accidental
interference and misuse of the data.
Conti…
 Encapsulation is the process of hiding the internal state of an
object and requiring all interactions to be performed through
an object’s methods. This approach:
• Provides better control over data.

• Prevents accidental modification of data.

• Promotes modular programming.

 Python achieves encapsulation through public, protected and


private attributes.
Conti…
How Encapsulation Works :
 Data Hiding: The variables (attributes) are kept private or
protected, meaning they are not accessible directly from
outside the class. Instead, they can only be accessed or
modified through the methods.

 Access through Methods: Methods act as the interface


through which external code interacts with the data stored in
the variables. For instance, getters and setters are common
methods used to retrieve and update the value of a private
variable.
Conti…
 Control and Security: By encapsulating the variables and
only allowing their manipulation via methods, the class can
enforce rules on how the variables are accessed or modified,
thus maintaining control and security over the data
Example of Encapsulation
 Encapsulation in Python is like having a bank account
system where your account balance (data) is kept private.

 You can’t directly change your balance by accessing the


account database.

 Instead, the bank provides you with methods (functions) like


deposit and withdraw to modify your balance safely.
Conti…

 Private Data (Balance): Your balance is stored securely.


Direct access from outside is not allowed, ensuring the
data is protected from unauthorized changes.

 Public Methods (Deposit and Withdraw): These are the


only ways to modify your balance. They check if your
requests (like withdrawing money) follow the rules (e.g.,
you have enough balance) before allowing changes.
Public Members
 Public members are accessible from anywhere, both inside and
outside the class. These are the default members in Python.
Explanation:
 Public Attribute (name): This attribute is declared without any
underscore prefixes. It is accessible from anywhere, both inside
and outside of the class.

 Public Method (display_name): This method is also accessible from


any part of the code. It directly accesses the public attribute and
prints its value.

 Object (obj): An instance of Public is created, and the display_name


method is called, demonstrating how public attributes and
methods can be accessed directly.

 Note: The __init__ method is a constructor and runs as soon as an


object of a class is instantiated.
Private members
 Private members are identified with a double underscore (_ _)
and cannot be accessed directly from outside the class.
Example
Conti…
Explanation:
 Private Attribute (__salary): This attribute is prefixed with two
underscores, which makes it a private member. Python enforces
privacy by name mangling, which means it renames the attribute
in a way that makes it hard to access from outside the class.

 Method (salary): This public method provides the only way to


access the private attribute from outside the class. It safely
returns the value of __salary.

 Direct Access Attempt: Trying to access the private attribute


directly (obj.__salary) will result in an AttributeError, showing that
direct access is blocked. This is Python’s way of enforcing
encapsulation at a language level.
Setters and Getters
 Why Use Getters and Setters?
 Encapsulation: Protect data by controlling how attributes
are accessed or modified.

 In Python, getters and setters are methods used to access


and update the values of private or protected attributes.

 Unlike some other programming languages, Python doesn't


have built-in support for private variables, but by
convention, an attribute prefixed with an underscore or
double underscore (e.g., _attribute or _ _attribute) is
treated as "private."
Conti…
 Setter to initialize value of private variables.

 Getter to return the value of private variables.


Example

You might also like