0% found this document useful (0 votes)
6 views

Chapter-V Object Oriented Programming

Python PPT Chapter 5 msbte
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Chapter-V Object Oriented Programming

Python PPT Chapter 5 msbte
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 114

PROGRAMMING WITH PYTHON

PWP - 22616

UNIT-V
OBJECT ORIENTED PROGRAMMING IN
PYTHON

Mr. Naresh A. Kamble


POINTS TO BE COVERED
• CREATING CLASSES AND OBJECTS
• METHOD OVERLOADING AND OVERWRITING
• DATA HIDING
• DATA ABSTRACTION
• INHERITANCE AND Child CLASSES
• CUSTOMIZATION VIA INHERITANCE SPECIALIZING
INHERITED METHOD

2
3
POINTS TO BE COVERED
• INTRODUCTION
• CLASS
• OBJECTS

4
5
CLASSES & OBJECTS

• Python is an object oriented programming language.

• Almost everything in Python is an object, with its properties


and methods.

• A Class is like an object constructor, or a "blueprint" for


creating 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

• Classes are created by keyword class.

• Variables are called as attributes.

• Attributes are the variables that belong to a class.

• Attributes are always public and can be accessed using the


dot (.) operator.

• Eg.:- Myclass.Myattribute

• Functions are called as methods.

9
CLASSES & OBJECTS

10
CLASSES & OBJECTS
• OBJECT

• An Object is an instance of a Class.

• A class is like a blueprint while an instance is a copy of the


class with actual values.

11
CLASSES & OBJECTS
• An object consists of :

• State: It is represented by the attributes of an object. It also


reflects the properties of an object.

• Behavior: It is represented by the methods of an object. It


also reflects the response of an object to other objects.

• Identity: It gives a unique name to an object and enables one


object to interact with other objects.
12
CLASSES & OBJECTS

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

• It is a special method that automatically gets called every time


objects are created.

• The __init__ method is similar to constructors in C++ and Java.

• Constructors are used to initializing the object’s state

19
CLASSES & OBJECTS

20
21
22
23
24
25
26
INHERITANCE
• DEFINITION

Inheritance allows us to inherit attributes and


methods from a parent class to child classes.

27
INHERITANCE
• PROPERTIES OF INHERITANCE

• It allows to derive a new class from old class.

• The old class is called as base class or parent class.

• The new class is called as derived class/subclass or child class.

• Inheritance allows subclasses to inherit all the variables and


methods to their parent class.

• Reusability of code is best advantage of inheritance.

28
INHERITANCE
• TYPES OF INHERITANCE

• SINGLE INHERITANCE

• MULTIPLE INHERITANCE

• MULTILEVEL INHERITANCE

• HIERARCHICAL INHERITANCE

• HYBRID INHERITANCE

29
INHERITANCE
• SINGLE INHERITANCE

• It includes only one parent class and one child class.

PARENT CLASS

CHILD CLASS
30
INHERITANCE
• SYNTAX

• SINGLE INHERITANCE

class SubClassName (ParentClass1[,ParentClass2,….,])

‘Optional class documentation string’

class_suite

31
INHERITANCE
• WRITE A PROGRAM

• Create two classes named Student() and Test() and student


information and marks. You can take input from user.

32
33
34
INHERITANCE
• MULTIPLE INHERITANCE

• It includes more than one parent class and the child class
inherits all properties of both parent class.

PARENT CLASS-1 PARENT CLASS-2

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

• In this inheritance a child class can act as parent class for


another child class

PARENT CLASS

CHILD CLASS-1

CHILD CLASS-2 40
41
42
43
44
INHERITANCE
• WRITE A PROGRAM

• Create three classes named Student(), Test() and Result() and


student information and marks. You can take input from user.

45
46
47
48
INHERITANCE
• HIERARCHICAL INHERITANCE

• It includes multiple inheritance from same parent class.

PARENT CLASS-1 PARENT CLASS-2

CHILD CLASS 1 CHILD CLASS 2


49
50
51
INHERITANCE
• HYBRID INHERITANCE

• If multiple inheritance occurs in a program then it is called


hybrid inheritance.

PARENT CLASS-1 PARENT CLASS-2

CHILD CLASS 1 CHILD CLASS 2


52
53
54
55
INHERITANCE
• SUPER FUNCTION IN INHERITANCE

• It directly calls the parent class method.

56
57
58
COMPOSITION CLASSES
• COMPOSITION CLASSES

• In this concept, a class has references to one or more objects of


other classes as an Instance variable.

• Here, by using the class name or by creating the object we can


access the members of one class inside another class.

• It enables creating complex types by combining objects of different


classes.

• It means that a class Child can contain an object of another class


Parent.

• This type of relationship is known as Has-A Relation. 59


COMPOSITION CLASSES
• The class name Child and Parent represents Has-A relation
between both of them.

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().

• Similarly, in Child class, we have one constructor in which we created an object


of Parent Class. Whenever we create an object of Child Class, the object of the
Parent class is automatically created.

• 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.

• Now, whenever we call m2() method of Child Class, automatically m1()


method of Parent Class will be called.

64
COMPOSITION CLASSES
• COMPOSITION CLASSES VS INHERITANCE

What should be used more INHERITANCE or


COMPOSITION ?

65
66
67
68
METHOD OVERLOADING
• Methods in Python can be called with zero, one, or more
parameters.

• This process of calling the same method in different ways is


called method overloading.

• Two methods cannot have the same name in Python; hence


method overloading is a feature that allows the same
operator to have different meanings.
69
70
71
72
METHOD OVERLOADING
• Now in above example, consider if you have to take any one
of following choice
– Only 1 input value

– Only 2 input values

– All 3 input values

• Then the program should give output in any choice and


should not give a traceback error.

• Reuse same code and modify necessary things to get desired


output.
73
74
75
76
77
METHOD OVERLOADING
• Example:
• Write a python program using Method Overloading Method to find
– Area of Rectangle

– 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

– Area of Square: - if only one argument is passed

– Area of Rectangle: - if two 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).

• When a method in a subclass has the same name, same


parameters or signature and same return type(or sub-type) as
a method in its super-class, then the method in the subclass
is said to override the method in the super-class.
82
83
84
85
86
87
88
89
90
91
92
DATA HIDING
• Data hiding is a concept which underlines the hiding of data or
information from the user.

• Data hiding is also known as information hiding.

• It includes object details such as data members, internal work.

• Data hiding excludes full data entry to class members and defends
object integrity by preventing unintended changes.

• Data hiding also minimizes system complexity for increase robustness


by limiting interdependencies between software requirements.

• In class, if we declare the data members as private so that no other


class can access the data members, then it is a process of hiding data.
93
94
95
96
97
DATA HIDING
• ADVANTAGES OF DATA HIDING:

• It helps to prevent damage or misuse of volatile data by hiding


it from the public.

• The class objects are disconnected from the irrelevant data.

• It isolates objects as the basic concept of OOP.

• It increases the security against hackers that are unable to


access important data.

98
DATA HIDING
• DISADVANTAGES OF DATA HIDING:

• It enables programmers to write lengthy code to hide


important data from common clients.

• 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.

• The main focus of data abstraction is to separate the interface


and the implementation of the program.

101
DATA ABSTRACTION
• In Python, abstraction can be achieved by using abstract
classes and interfaces.

• A class that consists of one or more abstract method is called


the abstract class.

• Abstract methods do not contain their implementation.

• Abstract class can be inherited by the subclass and abstract


method gets its definition in the subclass.

• Abstraction classes are meant to be the blueprint of the other


class.
102
DATA ABSTRACTION
• An abstract class can be useful when we are designing large
functions.

• An abstract class is also helpful to provide the standard


interface for different implementations of components.

103
DATA ABSTRACTION
• SYNTAX

We import the ABC class from the abc module.

104
DATA ABSTRACTION
• ABSTRACT BASE CLASSES

• An abstract base class is the common application program of


the interface for a set of subclasses.

• It can be used by the third-party, which will provide the


implementations such as with plugins.

• It is also beneficial when we work with the large code-base


hard to remember all the classes.
105
DATA ABSTRACTION
• WORKING OF THE ABSTRACT CLASSES

• Unlike the other high-level language, Python doesn't provide


the abstract class itself.

• We need to import the abc module, which provides the base


for defining Abstract Base classes (ABC).

• The ABC works by decorating methods of the base class as


abstract.
106
DATA ABSTRACTION
• WORKING OF THE ABSTRACT CLASSES

• It registers concrete classes as the implementation of the


abstract base.

• We use the @abstractmethod decorator to define an abstract


method or if we don't provide the definition to the method, it
automatically becomes the abstract method

107
108
109
DATA ABSTRACTION
• EXPLANATION OF CODE

• In the above code, we have imported the abc module to


create the abstract base class.

• We created the Car class that inherited the ABC class and
defined an abstract method named mileage().

• We have then inherited the base class from the three


different subclasses and implemented the abstract method
differently.

• We created the objects to call the abstract method.


110
DATA ABSTRACTION
• TRY YOURSELF

• Write a python program to display sides of following polygons

• Triangle, Pentagon, Hexagon, Square

• Simply display message of sides for each polygon e.g. for


triangle – “Triangle has 3 sides”

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.

• This base class inherited by the various subclasses.

• We implemented the abstract method in each subclass.

• 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

You might also like