PavanKumarPathipati
21 views
19 slides
Oct 20, 2024
Slide 1 of 19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
About This Presentation
OOPS stands for Object-Oriented Programming System or Object-Oriented Programming Structure. It's a programming paradigm that organizes code into objects that contain data and behavior.
Size: 554.34 KB
Language: en
Added: Oct 20, 2024
Slides: 19 pages
Slide Content
Object Oriented
Programming
- Python
Agenda
Python
•Introduction
•What is Class
•Inheritance and types
•Polymorphism
•Abstraction
•Encapsulation
2
Introduction
The main concept of OOPs is to bind the data and
the functions that work on that together as a
single unit so that no other part of the code can
access this data.
Python Class
A class is a collection of objects. A class contains the
blueprints or the prototype from which the objects are being
created.
python
3
Class
Syntax of Class:
class ClassName:
# Statement-1
.
# Statement-N
•Classes are created by keyword class.
•Attributes are the variables that belong to a class.
•Attributes are always public and can be accessed using the dot (.) operator.
python
4
Eg: class Dog:
sound = "bark"
…....
Object
•The object is an entity that has a state and behavior associated with it.
• It may be any real-world object like a mouse, keyboard, chair, table, pen, etc.
Integers, strings, floating-point numbers, even arrays, and dictionaries, are all
objects.
•More specifically, any single integer or any single string is an object.
•The number 12 is an object, the string “Hello, world” is an object
python
5
Object
Object Consists of:
•State
•Behavior
•Identity
Example:
•The identity can be considered as the name of the dog.
•State or Attributes can be considered as the breed, age, or color of the dog.
•The behavior can be considered as to whether the dog is eating or sleeping.
python
6
Constructors and Destructors
Constructors
• Used for instantiating an object.
•The task of constructors is to initialize(assign values)
to the data members of the class when an object of
the class is created.
•In Python the __init__() method is called the
constructor and is always called when an object is
created.
Syntax of constructor declaration :
def __init__(self):
# body of the constructor
Types:
Default Parameterized constructor
Destructors
•Destructors are called when an object gets destroyed.
• In Python, destructors are not needed as much as in C++ because
Python has a garbage collector that handles memory management
automatically.
•The __del__() method is a known as a destructor method in Python.
It is called when all references to the object have been deleted i.e
when an object is garbage collected.
Syntax of destructor declaration :
def __del__(self):
# body of destructor
Inheritance
Inheritance is defined as the mechanism of inheriting
the properties of the base class to the child class
The Parent class is the class which provides features
to another class. The parent class is also known
as Base class or Superclass.
The Child class is the class which receives features
from another class. The child class is also known as
the Derived Class or Subclass.
Python
9
Types of Inheritance
•Single
•Multiple
•Multilevel
•Hierarchical
•Hybrid
Single Inheritance
Single inheritance enables a derived class to
inherit properties from a single parent class.
10
PYTHON
Multiple Inheritance
•A class can be derived from more than one base class
this type of inheritance is called multiple inheritances.
• In multiple inheritances, all the features of the base
classes are inherited into the derived class.
11
PYTHON
Multilevel Inheritance
•In multilevel inheritance, features of the base class
and the derived class are further inherited into the
new derived class.
• This is similar to a relationship representing a
child and a grandfather.
12
PYTHON
Hierarchical Inheritance
•When more than one derived class are created from a
single base this type of inheritance is called hierarchical
inheritance.
•In this program, we have a parent (base) class and two or
more child(derived) classes.
13
PYTHON
Hybrid Inheritance
•Inheritance consisting of multiple
types of inheritance is called
hybrid inheritance.
14
PYTHON
Polymorphism
•Polymorphism simply means having many forms.
•This code demonstrates the concept of inheritance
and method overriding in Python classes.
• It shows how subclasses can override methods
defined in their parent class to provide specific
behavior while still inheriting other methods from
the parent class.
15
PYTHON
Encapsulation
•Encapsulation is a mechanism of wrapping the data (variables) and
code acting on the data (methods) together as a single unit.
•Access modifiers in the concept of Encapsulation and data hiding.
•Public: Accessible from inside or outside the class.
•Private: Accessible only inside class. Define a private member by
prefixing the member name with two underscores. Eg: __age
•Protected: Accessible. from inside the class and its sub-class.
Define a protected member by prefixing the member name with an
underscore Eg: _points
16
Access Modifiers
•Public
•Private
•Protected
Abstraction
•It hides unnecessary code details from the user.
• Also, when we do not want to give out sensitive
parts of our code implementation and this is where
data abstraction came.
17
Python
SUMMARY
Python
•These are various Object Oriented Programming
concepts in Python.
•Online Reference:
W3schools
GeeksforGeeks