Lec 05 OOP
Lec 05 OOP
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.
2
Introduction
Whereas procedural programming is centered on creating
procedures (functions), object oriented programming (OOP) is
centered on creating objects.
4
OOPs Concepts in Python
Class in Python
Objects in Python
Polymorphism in Python
Encapsulation in Python
Inheritance 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.
# Statement-1
# Statement-N
class Dog:
7
pass
Object
An object is any entity that has attributes and behaviors. For example, a
parrot is an object. It has
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
9
Cont..
10
Creating Classes and objects with methods
11
Adding attributes to a class
class Dog:
self.name = name
self.age = age
You can see that the function now takes two arguments after
self: name and age.
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.
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).
18
Polymorphism in Python
The word polymorphism means having many forms.
19
Cont..
Polymorphism has the following advantages:
20
21
22
Encapsulation in Python
Encapsulation is one of the fundamental concepts in object-
oriented programming (OOP).
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.
27
Cont..
Public Member
28
Cont..
Private Member
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.
For example, _project. This makes the project a protected variable that
can be accessed only by the child class.
32
33
End
Thanks
34