Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
78 views

OOPs in Python

The document discusses object oriented programming concepts in Python including classes, objects, attributes, methods, inheritance, and polymorphism. It defines a class as a template for real-world entities that can have properties and behaviors. Objects are specific instances of classes. Inheritance allows a child class to inherit attributes and methods from a parent class. The document provides examples of creating classes, instantiating objects, and using inheritance in Python code.

Uploaded by

Debashish Panda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

OOPs in Python

The document discusses object oriented programming concepts in Python including classes, objects, attributes, methods, inheritance, and polymorphism. It defines a class as a template for real-world entities that can have properties and behaviors. Objects are specific instances of classes. Inheritance allows a child class to inherit attributes and methods from a parent class. The document provides examples of creating classes, instantiating objects, and using inheritance in Python code.

Uploaded by

Debashish Panda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Python Object Oriented Programming

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Classes

Class is a template/blue-print for real-world entities

Properties Behavior

• Color • Make Calls


• Cost • Watch Videos
• Battery Life • Play Games

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Class in Python

Class is a user-defined data-type

int float

bool str

Mobile
Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Attributes and Methods

color
Attributes
cost

Play Game
Methods
Make Call

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Objects

Objects are specific instances of a class

Apple Motorola Samsung

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Objects in Python

Specific instances of Mobile data type

Apple Motorola Samsung

a = 10 b = 20 c = 30

Specific instances of integer data type

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Creating the first Class

Creating the ‘Phone’ class

Instantiating the ‘p1’ object

Invoking methods through object

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Adding parameters to the class

Setting and Returning the


attribute values

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Creating a class with Constructor

init method acts as the constructor

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Instantiating Object

Instantiating the ‘e1’ object

Invoking the
‘employee_details’
method

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Inheritance in Python

With inheritance one class can derive the properties of another class

Man inheriting
features from his
father

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Inheritance Example

Creating the base class

Instantiating the object for base class

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Inheritance Example

Creating the child class

Instantiating the object for child class

Invoking the child class method

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Over-riding init method

Over-riding init method

Invoking show_details()
method from parent class Invoking show_car_details()
method from child class

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Types of Inheritance

Single Inheritance

Multiple Inheritance

Multi-level Inheritance

Hybrid Inheritance

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Multiple Inheritance

In multiple inheritance, the child inherits from more than 1 parent class

Parent 1 Parent 2

Child

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Multiple Inheritance in Python

Parent Class One Child Class

Parent Class Two

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Multiple Inheritance in Python

Invoking methods

Instantiating object of child class

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Multi-Level Inheritance

In multi-level Inheritance, we have Parent, child, grand-child relationship

Parent

Child

Grand-Child

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Multi-Level Inheritance in Python

Grand-Child Class
Parent Class

Child Class

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Multi-Level Inheritance in Python

Invoking class methods

Instantiating object of GrandChild class

Proprietary content. © Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.

You might also like