Chapter Five
Class and Object Oriented Programming
1
Introduction
Procedural programming is a method of writing software. It is a
programming practice centered on the procedures or actions
that take place in a program.
Object-oriented programming is centered on objects.
Objects are created from abstract data types that encapsulate data
and functions together.
2
Introduction
Whereas procedural programming is centered on creating
procedures (functions), object oriented programming (OOP) is
centered on creating objects.
An object is a software entity that contains both data and
procedures. The data contained in an object is known as the
object’s data attributes.
An object’s data attributes are simply variables that reference
data.
The procedures that an object performs are known as methods.
3
An object’s methods are functions that perform operations on
Introduction
In Python object-oriented Programming (OOPs) is a
programming paradigm that uses objects and classes in
programming.
It aims to implement real-world entities like inheritance,
polymorphisms, encapsulation, etc. in the programming.
4
OOPs Concepts in Python
Class in Python
Objects in Python
Polymorphism in Python
Encapsulation in Python
Inheritance in Python
Data Abstraction in Python
5
Class
A class is code that specifies the data attributes and methods
for a particular type of object. An object is an instance of a
class.
It’s a “blueprint” from which objects may be created.
A class is a collection of objects.
Classes define functions called methods, which identify the
behaviors and actions that an object created from the class can
perform with its data.
6
Class
To define a class in Python, you can use the class keyword,
followed by the class name and a colon.
Class Definition Syntax:
class ClassName:
# Statement-1
# Statement-N
Creating an empty class in python
class Dog:
7
pass
Object
An object is any entity that has attributes and behaviors. For example, a
parrot is an object. It has
attributes - name, age, color, etc.
behavior - dancing, singing, etc.
class Dog:
pass
Creating an Object
This will create an object named obj of the class Dog defined above.
obj = Dog()
8
The Python __init__ Method
The __init__ method is similar to constructors in C++ and Java.
It is run as soon as an object of a class is instantiated.
The method is useful to do any initialization you want to do with
your object. Now let us define a class and create some objects
using the self and __init__ method.
Creating a class and object with class and instance attributes
9
Cont..
10
Creating Classes and objects with methods
11
Adding attributes to a class
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
You can see that the function now takes two arguments after
self: name and age.
These then get assigned to self.name and self.age respectively.
You can now create a new ozzy object with a name and age:
12
ozzy = Dog("Ozzy", 2)
Cont..
To access an object's attributes in Python, you can use the dot
notation. This is done by typing the name of the object, followed
by a dot and the attribute's name
print(ozzy.name)
print(ozzy.age)
13
Define methods in a class
Now that you have a Dog class, it does have a name and age,
which you can keep track of, but it doesn't actually do anything.
This is where instance methods come in. You can rewrite the
class to now include a bark() method. Notice how the def
keyword is used again, as well as the self argument.
14
Inheritance in Python
Inheritance is the capability of one class to derive or inherit the
properties from another class.
The class that derives properties is called the derived class or
child class and the class from which the properties are being
derived is called the base class or parent class.
It provides the reusability of a code.
15
Cont..
Python Inheritance Syntax:
Class BaseClass:
{Body}
Class DerivedClass(BaseClass):
{Body}
16
17
Cont..
We have created two classes i.e. Person (parent class) and
Employee (Child Class).
The Employee class inherits from the Person class.
We can use the methods of the person class through the
employee class as seen in the display function in the above
code.
18
Polymorphism in Python
The word polymorphism means having many forms.
In programming, polymorphism means the same function name
(but different signatures) being used for different types.
Polymorphism is often used in Class methods, where we can
have multiple classes with the same method name.
The key difference is the data types and number of arguments
used in function.
19
Cont..
Polymorphism has the following advantages:
It is beneficial to reuse the codes.
The codes are simple to debug.
A single variable can store multiple data types.
20
21
22
Encapsulation in Python
Encapsulation is one of the fundamental concepts in object-
oriented programming (OOP).
It describes the idea of wrapping data and the methods that
work on data within one unit.
This puts restrictions on accessing variables and methods
directly and can prevent the accidental modification of data.
Declare data members as private in the class to make it
inaccessible outside.
Encapsulation is a combination of data hiding and abstraction.
23
24
Cont..
Now let’s use Encapsulation to hide an object’s internal
representation from the outside and make things secure.
Whenever we work in a team and deal with sensitive data, it’s
not ideal to provide access to all variables used within the class.
Encapsulation is achieved by using Access Modifiers -
declaring a class’s data members and methods as either private
or protected.
25
Access Modifiers
Access modifiers are used to limit access to the variables and
methods of a class. Python provides three types of access
modifiers public, private, and protected.
Public Member: Accessible anywhere from outside the class.
Private Member: Accessible within the class.
Protected Member: Accessible within the class and its sub-
classes.
In Python, we do not have keywords like public, private, and
protected, as in the case of Java. Instead, we achieve this by
26
Cont..
27
Cont..
Public Member
Public Members can be accessed from outside and within the
class. Making it easy to access by all. By default, all the
member variables of the class are public.
28
Cont..
Private Member
We can protect variables in the class
by marking them private. To make any
variable a private just add two
underscores as a prefix at the start of
its name. For example, __salary.
Private members are accessible only
within the class and cannot be
accessed from the objects of the class.
29
Cont..
NOTE: The output of the above code will throw an error, since
we are trying to access a private variable that is hidden from
the outside.
How to access private methods outside the class?
Add private members inside a public method
You can declare a public method inside the class which uses a
private member and call the public method containing a private
member outside the class.
30
31
Cont..
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.
For example, _project. This makes the project a protected variable that
can be accessed only by the child class.
32
33
End
Thanks
34