0% found this document useful (0 votes)
192 views28 pages

19.python OOPs Concepts

The document discusses Python object-oriented programming concepts. It explains that Python supports OOP through classes, objects, methods, inheritance, polymorphism, encapsulation, and abstraction. A class defines common attributes and behaviors of objects. An object is an instance of a class that contains state and behavior. Methods are functions defined inside a class. Inheritance allows a child class to inherit attributes and behaviors from a parent class. Polymorphism means the same method can act differently depending on the object. Encapsulation restricts access to attributes and methods. Abstraction hides unnecessary details and exposes only essential functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
192 views28 pages

19.python OOPs Concepts

The document discusses Python object-oriented programming concepts. It explains that Python supports OOP through classes, objects, methods, inheritance, polymorphism, encapsulation, and abstraction. A class defines common attributes and behaviors of objects. An object is an instance of a class that contains state and behavior. Methods are functions defined inside a class. Inheritance allows a child class to inherit attributes and behaviors from a parent class. Polymorphism means the same method can act differently depending on the object. Encapsulation restricts access to attributes and methods. Abstraction hides unnecessary details and exposes only essential functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 28

Python OOPs Concepts

OPPs Concepts
• Like other general-purpose programming languages, Python is also an
object-oriented language since its beginning. It allows us to develop
applications using an Object-Oriented approach. In Python, we can
easily create and use classes and objects.
• An object-oriented paradigm is to design the program using classes
and objects. The object is related to real-word entities such as book,
house, pencil, etc. The oops concept focuses on writing the reusable
code. It is a widespread technique to solve the problem by creating
objects.
• Class
• Object
• Method
• Inheritance
• Polymorphism
• Data Abstraction
• Encapsulation
Class
• The class can be defined as a collection of objects. It is a logical entity
that has some specific attributes and methods. For example: if you have
an employee class, then it should contain an attribute and method, i.e. an
email id, name, age, salary, etc.
• Syntax
class ClassName:
<statement-1>
.
.
<statement-N>
Object
• The object is an entity that has state and behavior. It may be any real-
world object like the mouse, keyboard, chair, table, pen, etc.
• Everything in Python is an object, and almost everything has attributes
and methods. All functions have a built-in attribute __doc__, which
returns the docstring defined in the function source code.
Example
class car:
def __init__(self,modelname, year):
self.modelname = modelname
self.year = year
def display(self):
print(self.modelname,self.year)

c1 = car("Toyota", 2016)
c1.display()
Method
• The method is a function that is associated with an object. In Python, a
method is not unique to class instances. Any object type can have
methods.
Inheritance
• Inheritance is the most important aspect of object-oriented
programming, which simulates the real-world concept of inheritance.
It specifies that the child object acquires all the properties and
behaviors of the parent object.
• By using inheritance, we can create a class which uses all the
properties and behavior of another class. The new class is known as a
derived class or child class, and the one whose properties are acquired
is known as a base class or parent class.
• It provides the re-usability of the code.
Polymorphism
• Polymorphism contains two words "poly" and "morphs". Poly means
many, and morph means shape. By polymorphism, we understand that
one task can be performed in different ways. For example - you have a
class animal, and all animals speak. But they speak differently. Here,
the "speak" behavior is polymorphic in a sense and depends on the
animal. So, the abstract "animal" concept does not actually "speak",
but specific animals (like dogs and cats) have a concrete
implementation of the action "speak".
Encapsulation
• Encapsulation is also an essential aspect of object-oriented
programming. It is used to restrict access to methods and variables. In
encapsulation, code and data are wrapped together within a single unit
from being modified by accident.
Data Abstraction
• Data abstraction and encapsulation both are often used as synonyms.
Both are nearly synonyms because data abstraction is achieved
through encapsulation.
• Abstraction is used to hide internal details and show only
functionalities. Abstracting something means to give names to things
so that the name captures the core of what a function or a whole
program does.
Object-oriented vs. Procedure-oriented
Programming languages
Index Object-oriented Programming Procedural Programming
1. Object-oriented programming is the problem- Procedural programming uses a list of instructions to do
solving approach and used where computation is computation step by step.
done by using objects.
2. It makes the development and maintenance easier. In procedural programming, It is not easy to maintain
the codes when the project becomes lengthy.

3. It simulates the real world entity. So real-world It doesn't simulate the real world. It works on step by
problems can be easily solved through oops. step instructions divided into small parts called
functions.

4. It provides data hiding. So it is more secure than Procedural language doesn't provide any proper way for
procedural languages. You cannot access private data binding, so it is less secure.
data from anywhere.
5. Example of object-oriented programming Example of procedural languages are: C, Fortran,
languages is C++, Java, .Net, Python, C#, etc. Pascal, VB etc.
Classes and Objects in Python
• Suppose a class is a prototype of a building. A building contains all the
details about the floor, rooms, doors, windows, etc. we can make as
many buildings as we want, based on these details. Hence, the
building can be seen as a class, and we can create as many objects of
this class.
• On the other hand, the object is the instance of a class. The process of
creating an object can be called instantiation.
• In this section of the tutorial, we will discuss creating classes and
objects in Python. We will also discuss how a class attribute is
accessed by using the object.
Creating Classes in Python
In Python, a class can be created by using the keyword class, followed
by the class name. The syntax to create a class is given below.

class ClassName:
#statement_suite
Example
class Employee:
id = 10
name = "Devansh"
def display (self):
print(self.id,self.name)
• Here, the self is used as a reference variable, which refers to the
current class object. It is always the first argument in the function
definition. However, using self is optional in the function call.
The self-parameter
• The self-parameter refers to the current instance of the class and
accesses the class variables. We can use anything instead of self, but it
must be the first parameter of any function which belongs to the class.
Creating Objects (instance) in Python
• A class needs to be instantiated if we want to use the class attributes in
another class or method. A class can be instantiated by calling the class
using the class name.
• The syntax to create the instance of the class is given below.
<object-name> = <class-name>(<arguments>)
Example
class Employee:
id = 10
name = "John"
def display (self):
print("ID: %d \nName: %s"%(self.id,self.name))
# Creating a emp instance of Employee class
emp = Employee()
emp.display()
Delete the Object
• We can delete the properties of the object or object itself by using the
del keyword.
class Employee:
id = 10
name = "John"
def display(self):
print("ID: %d \nName: %s" % (self.id, self.name))
# Creating a emp instance of Employee class
emp = Employee()
# Deleting the property of object
del emp.id
# Deleting the object itself
del emp
emp.display()
Python Constructor
• A constructor is a special type of method (function) which is used to
initialize the instance members of the class.
• In C++ or Java, the constructor has the same name as its class, but it
treats constructor differently in Python. It is used to create an object.
• Constructors can be of two types.
1.Parameterized Constructor
2.Non-parameterized Constructor
• Constructor definition is executed when we create the object of this
class. Constructors also verify that there are enough resources for the
object to perform any start-up task.
Creating the constructor in python
• In Python, the method the __init__() simulates the constructor of the
class. This method is called when the class is instantiated. It accepts
the self-keyword as a first argument which allows accessing the
attributes or method of the class.
• We can pass any number of arguments at the time of creating the class
object, depending upon the __init__() definition. It is mostly used to
initialize the class attributes. Every class must have a constructor, even
if it simply relies on the default constructor.
lass Employee:
def __init__(self, name, id):
self.id = id
self.name = name
def display(self):
print("ID: %d \nName: %s" % (self.id, self.name))
emp1 = Employee("John", 101)
emp2 = Employee("David", 102)
# accessing display() method to print employee 1 information
emp1.display()
# accessing display() method to print employee 2 information
emp2.display()
Counting the number of objects of a class
class Student:
count = 0
def __init__(self):
Student.count = Student.count + 1
s1=Student()
s2=Student()
s3=Student()
print("The number of students:",Student.count)
Python Non-Parameterized Constructor
• The non-parameterized constructor uses when we do not want to manipulate the
value or the constructor that has only self as an argument. Consider the following
example.
class Student:
# Constructor - non parameterized
def __init__(self):
print("This is non parametrized constructor")
def show(self,name):
print("Hello",name)
student = Student()
student.show("John")
Python Parameterized Constructor
• The parameterized constructor has multiple parameters along with the self. Consider
the following example.
class Student:
# Constructor - parameterized
def __init__(self, name):
print("This is parametrized constructor")
self.name = name
def show(self):
print("Hello",self.name)
student = Student("John")
student.show()
Python Default Constructor
• When we do not include the constructor in the class or forget to declare it, then that becomes
the default constructor. It does not perform any task but initializes the objects. Consider the
following example.
class Student:
roll_num = 101
name = "Joseph"

def display(self):
print(self.roll_num,self.name)

st = Student()
st.display()
Python built-in class functions
SN Function Description

1 getattr(obj,name,default) It is used to access the attribute of the object.

2 setattr(obj, name,value) It is used to set a particular value to the specific attribute of an object.

3 delattr(obj, name) It is used to delete a specific attribute.

4 hasattr(obj, name) It returns true if the object contains some specific attribute.

You might also like