Unit-3 - OOP in Python - Updated
Unit-3 - OOP in Python - Updated
Unit-3
Object-Oriented Programming and
Multi-threading:
What Is Object-Oriented
Programming (OOPs) in python?
In Python, OOPs stands for Object-Oriented Programming. It is a
programming paradigm that focuses on the use of objects and
classes to create programs.
1) Class
2) Objects
3) Polymorphism
4) Encapsulation
5) Inheritance
6) Data Abstraction
What Is Object-Oriented
Programming (OOPs) in python?
Example of OOPs :
For example, a car can be an object. If we consider the car as an
object, its properties would be its color, model, price, brand,
etc. And its behavior/function would be acceleration, slowing
down, and gear change.
Another example – If we consider a dog as an object then its
properties would be its color, breed, name, weight, etc., and its
behavior/function would be walking, barking, playing, etc.
Object-Oriented Programming is famous because it implements
real-world entities like objects, hiding, inheritance, etc, in
programming. It makes visualization easier because it is close to
real-world scenarios.
What Is a Class?
•A straightforward answer to this question is- A class is a
collection of objects. Unlike primitive data structures, classes
are data structures that the user defines. They make the code
more manageable.
Objects:
•When we define a class, only the description or a blueprint of the
object is created. There is no memory allocation until we create
its object. The objector instance contains real data or information.
•Instantiation is nothing but creating a new object/instance of a class.
Let’s create the object of the above class we defined-
obj1 = Car()
Class Constructor
•The job of the class constructor is to assign the values to the
data members of the class when an object of the class is
created.
•There can be various properties of a car, such as its name, color,
model, brand name, engine power, weight, price, etc. We’ll
choose only a few for understanding purposes.
class Car:
def __init__(self, name, color):
self.name = name
self.color = color
Class Constructor
• So, the properties of the car or any other object must be inside
a method that we call __init__( ).
• This __init__() method is also known as the constructor
method. We call a constructor method whenever an object of
the class is constructed.
• Now let’s talk about the parameter of the __init__() method. So,
the first parameter of this method has to be self. Then only will
the rest of the parameters come.
• The two statements inside the constructor method are –
self.name = name
self.color = color:
Class Constructor
•This will create new attributes, namely name and color, and
then assign the value of the respective parameters to them.
•By using the “self” keyword, we can access the attributes and
methods of the class.
def deposit(self):
amount=float(input("Enter amount to be Deposited: "))
self.balance += amount
print("\n Amount Deposited:",amount)
def withdraw(self):
amount = float(input("Enter amount to be Withdrawn: "))
if self.balance>=amount:
self.balance-=amount
print("\n You Withdrew:", amount)
else:
print("\n Insufficient balance ")
def display(self):
print("\n Net Available Balance=",self.balance)
s = Bank_Account()
s.deposit()
s.withdraw()
s.display()
Special methods
__init__() – constructor
__del__() – destructor, del ob
__str__() – string representation of object,
print ob
__len__() – length of object, len(ob)
Principles of OOP (A.E.I.P)
• There are mainly four OOP Principles
Abstraction
Encapsulation
Inheritance
Polymorphism
Abstraction
• Abstraction refers to the act of representing
essential features without including the
background details or explanations.
• Abstraction provides you a generalized view of
your classes or object by providing relevant
information.
• Abstraction is the process of hiding the working
style of an object, and showing the information
of an object in understandable manner.
Abstraction Example
Abstract information (Necessary and
Common Information) for the object
“Mobile Phone” is make a call to any
number and can send SMS.”
FM Radio FM Radio
MP3 MP3
Camera Camera
Video Recording
Reading E-mails
Abstraction Example
• Example:
If somebody in your collage tell you to fill application form, you
will fill your details like name, address, data of birth,
which semester, percentage you have got etc.
• If some doctor gives you an application to fill the details, you will
fill the details like name, address, date of birth, blood
group, height and weight.
• See in the above example what is the common thing?
Age, name, address so you can create the class which
consist of common thing that is called abstract class.
That class is not complete and it can inherit by other class.
Encapsulation
• The wrapping up of data and functions into a
single unit is known as encapsulation
• The insulation of the data from direct access
by the program is called data hiding or
information hiding.
• It is the process of enclosing one or more
details from outside world through access
right.
Encapsulation
A B
C
Continue..
• Encapsulation is one of the key features of object-
oriented programming. Encapsulation refers to
the bundling of attributes and methods inside a
single class.
• It prevents outer classes from accessing and
changing attributes and methods of a class. This
also helps to achieve data hiding.
• In Python, we denote private attributes using
underscore as the prefix i.e single _ or double __.
For example,
What is Encapsulation in Python?
• Encapsulation in Python describes the concept
of bundling data and methods within a single
unit.
• So, for example, when you create a class, it
means you are implementing encapsulation.
• A class is an example of encapsulation as it
binds all the data members (instance
variables) and methods into a single unit.
Continue ..
Example:
In this example, we create an Employee class by defining employee attributes
such as name and salary as an instance variable and implementing behavior
using work() and show() instance methods.
In the above program,
we defined a
Computer class.
We used __init__()
method to store the
maximum selling price
of Computer.
Inheritance
• Inheritance is the process by which objects of one class
acquire the properties of objects of another class.
Vehicle
Syntax of a constructor:
Continue ..
Where,
• Default Constructor
• Non-parametrized constructor
• Parameterized constructor
Continue ..
Default Constructor
Non-Parametrized Constructor
Parametrized Constructor
What is Destructor in Python?
• A destructor is called when an object is
deleted or destroyed.
• Destructor is used to perform the clean-up
activity before destroying the object, such as
closing database connections or filehandle.
Access Modifiers in Python
• Encapsulation can be achieved by declaring the data
members and methods of a class either as private or
protected. But In Python, we don’t have direct access
modifiers like public, private, and protected. We can
achieve this by using single
underscore and double underscores.
• Access modifiers limit access to the variables and
methods of a class. Python provides three types of
access modifiers private, public, and protected.
• Public Member: Accessible anywhere from outside of
class.
• Private Member: Accessible within the class
• Protected Member: Accessible within the class and its
sub-classes
Continue ..
Public member
Private Member
• We can protect variables in the class by
marking them private. To define a private
variable add two underscores as a prefix at the
start of a variable name.
• Private members are accessible only within
the class, and we can’t access them directly
from the class objects.
Private Member
Private Member (Public method to access private members)
Protected Member
• Protected members are accessible within the
class and also available to its sub-classes. To
define a protected member, prefix the
member name with a single underscore _.
• Protected data members are used when you
implement inheritance and want to allow data
members access to only child classes.
Protected Member
Method Overloading
• The process of calling the same method with
different parameters is known as method
overloading.
• Method overloading is the concept of
polymorphism
• It works in the same method names and
different arguments.
• Arguments different will ne based on a
number of arguments and types of
arguments.
Method Overriding
• Method overriding is the method having the
same name with the same arguments.
• It is implemented with the inheritance also..
• It mostly used for memory reducing processes.
• Using method overriding polymorphism allows
us to defines methods in the child class that have
the same name as the methods in the parent
class.
• This process of re-implementing the inherited
method in the child class is known as Method
Overriding.
Example
Output
Operator Overloading in Python
• Operator overloading means changing the
default behavior of an operator depending on
the operands (values) that we use.
• In other words, we can use the same operator
for multiple purposes.
• For example, the + operator will perform an
arithmetic addition operation when used with
numbers. Likewise, it will perform
concatenation when used with strings.