Chapter-V Object Oriented Programming
Chapter-V Object Oriented Programming
PWP - 22616
UNIT-V
OBJECT ORIENTED PROGRAMMING IN
PYTHON
2
3
POINTS TO BE COVERED
• INTRODUCTION
• CLASS
• OBJECTS
4
5
CLASSES & OBJECTS
6
CLASSES & OBJECTS
Think of a class as a blueprint of a house. It
contains all the details about floors, doors,
windows etc. Based on this description we
build the house. House is an object
7
CLASSES & OBJECTS
• CLASS
• A class is a user-defined blueprint or prototype from which objects
are created.
• Classes provide a means of bundling data and functionality
together.
• Creating a new class creates a new type of object, allowing new
instances of that type to be made.
• Each class instance can have attributes attached to it for
maintaining its state.
• Class instances can also have methods (defined by their class) for
modifying their state.
8
CLASSES & OBJECTS
• IMPORTANT POINTS FOR CLASS
• Eg.:- Myclass.Myattribute
9
CLASSES & OBJECTS
10
CLASSES & OBJECTS
• OBJECT
11
CLASSES & OBJECTS
• An object consists of :
13
CLASSES & OBJECTS
14
CLASSES & OBJECTS
• THE self METHOD
• Class methods must have an extra first parameter in the method
definition.
• We do not give a value for this parameter when we call the
method, Python provides it.
• If we have a method that takes no arguments, then we still have to
have one argument.
• When we call a method of this object as myobject.method(arg1,
arg2), this is automatically converted by Python into
MyClass.method(myobject, arg1, arg2) – this is all the special self
is about.
15
16
CLASSES & OBJECTS
17
CLASSES & OBJECTS
18
CLASSES & OBJECTS
• THE __init__ METHOD
19
CLASSES & OBJECTS
20
21
22
23
24
25
26
INHERITANCE
• DEFINITION
27
INHERITANCE
• PROPERTIES OF INHERITANCE
28
INHERITANCE
• TYPES OF INHERITANCE
• SINGLE INHERITANCE
• MULTIPLE INHERITANCE
• MULTILEVEL INHERITANCE
• HIERARCHICAL INHERITANCE
• HYBRID INHERITANCE
29
INHERITANCE
• SINGLE INHERITANCE
PARENT CLASS
CHILD CLASS
30
INHERITANCE
• SYNTAX
• SINGLE INHERITANCE
class_suite
31
INHERITANCE
• WRITE A PROGRAM
32
33
34
INHERITANCE
• MULTIPLE INHERITANCE
• It includes more than one parent class and the child class
inherits all properties of both parent class.
CHILD CLASS
35
INHERITANCE
• MULTIPLE INHERITANCE
• When a class is derived from more than one base class it is called
multiple Inheritance. The derived class inherits all the features of
the base case.
Class Base1:
Body of the class
Class Base2
Body of the class
Class Derived(Base1, Base2):
Body of the class
36
37
38
39
INHERITANCE
• MULTILEVEL INHERITANCE
PARENT CLASS
CHILD CLASS-1
CHILD CLASS-2 40
41
42
43
44
INHERITANCE
• WRITE A PROGRAM
45
46
47
48
INHERITANCE
• HIERARCHICAL INHERITANCE
56
57
58
COMPOSITION CLASSES
• COMPOSITION CLASSES
CHILD CLASS
HAS A
PARENT CLASS
60
61
62
63
COMPOSITION CLASSES
• In the above example, we created two classes Parent and Child to show the
Has-A Relation among them.
• In the Parent class, we have one constructor and an instance method m1().
• Now in m2() method of Child class we are calling m1() method of Parent Class
using instance variable obj1 in which reference of Parent Class is stored.
64
COMPOSITION CLASSES
• COMPOSITION CLASSES VS INHERITANCE
65
66
67
68
METHOD OVERLOADING
• Methods in Python can be called with zero, one, or more
parameters.
– Area of Square
• Create only one class and define only one method in that class and
generate output in following manner.
– Nothing to Print – if no arguments are passed
78
79
80
81
METHOD OVERRIDING
• Method overriding is an ability of any object-oriented
programming language that allows a subclass or child class to
provide a specific implementation of a method that is already
provided by one of its super-class(s) or parent class(s).
• Data hiding excludes full data entry to class members and defends
object integrity by preventing unintended changes.
98
DATA HIDING
• DISADVANTAGES OF DATA HIDING:
• The linkage between the visible and invisible data makes the
objects work faster, but data hiding prevents this linkage.
99
100
DATA ABSTRACTION
• The process by which data and functions are defined in such a
way that only essential details can be seen and unnecessary
implementations are hidden is called Data Abstraction.
101
DATA ABSTRACTION
• In Python, abstraction can be achieved by using abstract
classes and interfaces.
103
DATA ABSTRACTION
• SYNTAX
104
DATA ABSTRACTION
• ABSTRACT BASE CLASSES
107
108
109
DATA ABSTRACTION
• EXPLANATION OF CODE
• We created the Car class that inherited the ABC class and
defined an abstract method named mileage().
111
112
113
DATA ABSTRACTION
• EXPLANATION OF CODE
• In the above code, we have defined the abstract base class named Polygon
and we also defined the abstract method.
• We created the object of the subclasses and invoke the sides() method.
• The hidden implementations for the sides() method inside the each
subclass comes into play.
• The abstract method sides() method, defined in the abstract class, is never
invoked.
114