0% found this document useful (0 votes)
1 views42 pages

Python Object-Oriented Programming (Oop) - Pynative

This document provides an overview of Object-Oriented Programming (OOP) concepts in Python, including classes, objects, encapsulation, inheritance, and polymorphism. It explains the structure of classes, the role of constructors, and the use of access modifiers to manage data visibility. Additionally, it covers various types of inheritance and the implementation of polymorphism through method overriding and built-in functions.

Uploaded by

niki.kovalkovic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views42 pages

Python Object-Oriented Programming (Oop) - Pynative

This document provides an overview of Object-Oriented Programming (OOP) concepts in Python, including classes, objects, encapsulation, inheritance, and polymorphism. It explains the structure of classes, the role of constructors, and the use of access modifiers to manage data visibility. Additionally, it covers various types of inheritance and the implementation of polymorphism through method overriding and built-in functions.

Uploaded by

niki.kovalkovic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

OOP

Python Object-Oriented Programming (OOP) – PYnative


Python Object-Oriented
Programming (OOP)
• In this series, you will learn OOP (Object
Oriented Programming) in Python. OOP
concepts include:
• object, classes
• constructor and encapsulation
• Polymorphism
• Inheritance
• Aggregation
Classes and Objects in Python
• Python is an object-oriented programming language.
This means that almost all the code is implemented
using a special construct called classes. A class is a
code template for creating objects.
•Class: The class is a user-defined data structure that
binds the data members and methods into a single unit.
Class is a blueprint or code template for object
creation. Using a class, you can create as many objects
as you want.
•Object: An object is an instance of a class. It is a
collection of attributes (variables) and methods. We use
the object of a class to perform actions.
Classes and Objects in Python
Create a Class in Python

•class_name: It is the name of the class


•Docstring: It is the first string inside the
class and has a brief
description of the class. Although not mandatory, this is highly
recommended.
•statements: Attributes and methods
Example: Define a class in
Python
• In this example, we are creating a Person Class with
name, sex, and profession instance variables.
Create Object of a Class
• When we create an object of the class, it is called
instantiation. The object is also called the instance of
a class.
• A constructor is a special method used to create and
initialize an object of a class. This method is defined in
the class.
• And, using the __init__() method we can implement
constructor to initialize the object.
Class Attributes
When we design a class, we use instance variables and class
variables.
In Class, attributes can be defined into two parts:
•Instance variables: The instance variables are attributes
attached to an instance of a class. We define instance variables in
the constructor ( the __init__() method of a class).
•Class Variables: A class variable is a variable that is declared
inside of class, but outside of any instance method
or __init__() method.
Class Methods
• In Object-oriented programming, Inside a Class, we can
define the following this types of methods.
• Instance method: Used to access or modify the object
state. If we use instance variables inside a method, such
methods are called instance methods.
• Class method: Used to access or modify the class state. In
method implementation, if we use only class variables, then
such type of methods we should declare as a class method.
Object Properties
• Modify Object Properties

• Delete object properties


Delete Objects
In Python, we can also
delete the object by using
a del keyword. An object can
be anything like, class
object, list, tuple, set, etc.
Constructors in Python
• In object-oriented programming, A constructor is a
special method used to create and initialize an
object of a class. This method is defined in the class
Types of Constructors
• Python will provide a
default constructor if no
constructor is defined.
• A constructor without any
arguments is called a non-
parameterized constructor.
This type of constructor is
used to initialize each
object with default values.
• A constructor with defined
parameters or arguments
is called a parameterized
constructor. We can pass
different values to each
object at the time of
creation using a
parameterized constructor.
Examples
Constructor Overloading
• Python does not support constructor overloading.
If we define multiple constructors then, the interpreter
will considers only the last constructor and throws an
error if the sequence of the arguments doesn’t match as
per the last constructor. The following example shows
the same.
Counting the Number of objects
of a Class
• for counting the number of objects of a class, we can
add a counter in the constructor, which increments by
one after each object creation.
Encapsulation in Python
• Encapsulation in Python describes the concept of bundling
data and methods within a single unit. So, for
example, when you create a class, it means you are
implementing encapsulation.
• A class is an example of encapsulation as it binds all the
data members (instance variables) and methods into a
single unit.
• Using encapsulation, we can hide an object’s internal
representation from the outside. This is called information
hiding.
• Also, encapsulation allows us to restrict accessing variables
and methods directly and prevent accidental data
modification by creating private data members and
Encapsulation in Python
• Encapsulation is a way to can restrict access to methods and
variables from outside of class. Whenever we are working
with the class and dealing with sensitive data, providing
access to all variables used within the class is not a good
choice.
• Access modifiers limit access to the variables and methods
of a class. Python provides three types of access modifiers
private, public, and protected.
• Public Member: Accessible anywhere from outside of a class.
• Private Member: Accessible within the class
• Protected Member: Accessible within the class and its sub-classe
Access Modifiers in Python
Public Member
• Public data
members are
accessible within
and outside of a
class. All member
variables of the
class are by
default public.
Private Member
• Private members
are accessible only
within the class,
and we can’t
access them
directly from the
class objects.
Private Member
• We can access private members from outside of a class
using the following two approaches
• Create public method to access private members
• Use name mangling
Protected Member

• Protected
members are
accessible within
the class and also
available to its
sub-classes. To
define a protected
member, prefix
the member name
with a single
underscore _.
Getters and Setters in Python
• To implement proper encapsulation in Python, we need to
use setters and getters.
• The primary purpose of using getters and setters in object-
oriented programs is to ensure data encapsulation. Use the
getter method to access data members and the setter
methods to modify the data members.
• In Python, private variables are not hidden fields like in
other programming languages. The getters and setters
methods are often used when:
• When we want to avoid direct access to private variables
• To add validation logic for setting a value
Advantages of Encapsulation
• Security: The main advantage of using encapsulation is the security of
the data. Encapsulation protects an object from unauthorized access. It
allows private and protected access levels to prevent accidental data
modification.
• Data Hiding: The user would not be knowing what is going on behind
the scene. They would only be knowing that to modify a data member,
call the setter method. To read a data member, call the getter method.
What these setter and getter methods are doing is hidden from them.
• Simplicity: It simplifies the maintenance of the application by keeping
classes separated and preventing them from tightly coupling with each
other.
• Aesthetics: Bundling data and methods within a class makes code
more readable and maintainable
Inheritance in Python
• The process of inheriting the properties
of the parent class into a child class is
called inheritance. The existing class is
called a base class or parent class and the
new class is called a subclass or child class or
derived class.
• In Object-oriented programming, inheritance is
an important aspect. The main purpose of
inheritance is the reusability of code because
we can use the existing class to create a new
class instead of creating it from scratch.
• In inheritance, the child class acquires all the
data members, properties, and functions from
the parent class. Also, a child class can also
provide its specific implementation to the
methods of the parent class.
Types Of Inheritance
In Python, based upon the number of child and parent
classes involved, there are five types of inheritance.
The type of inheritance are listed below:
1.Single inheritance
2.Multiple Inheritance
3.Multilevel inheritance
4.Hierarchical Inheritance
5.Hybrid Inheritance
Single Inheritance
• In single inheritance, a child class inherits from a single-
parent class. Here is one child class and one parent
class.
Multiple Inheritance
• In multiple inheritance, one child class can inherit from
multiple parent classes. So here is one child class and
multiple parent classes
Multilevel inheritance
• In multilevel inheritance, a
class inherits from a child
class or derived class.
Suppose three classes A, B,
C. A is the superclass, B is
the child class of A, C is the
child class of B. In other
words, we can say a chain
of classes is called
multilevel inheritance.
Hierarchical Inheritance
• In Hierarchical inheritance, more than one child class is
derived from a single parent class. In other words, we
can say one parent class and multiple child classes.
Python super() function

In child class, we can refer to parent class by using


the super() function. The super function returns a
temporary object of the parent class that allows us to
call a parent class method inside a child class method.

Benefits of using the super() function.


1.We are not required to remember or specify the
parent class name to access its methods.
2.We can use the super() function in
both single and multiple inheritances.
3.The super() function support code reusability as
there is no need to write the entire function
Using __init__
method from
parent class
Using other
methods from
parent class
Activity -
Python OOP Exercise – Classes and
•Objects Exercises
OOP Exercise (pynative.com)
1: Create a Class with instance attributes
• OOP Exercise 2: Create a Vehicle class without any varia
bles and methods
• OOP Exercise 3: Create a child class Bus that will inherit
all of the variables and methods of the Vehicle class
• OOP Exercise 4: Class Inheritance
• OOP Exercise 5: Define a property that must have the s
ame value for every class instance (object)
• OOP Exercise 6: Class Inheritance
• OOP Exercise 7: Check type of an object
• OOP Exercise 8: Determine if School_bus
is also an instance of the Vehicle class
Polymorphism in Python
• Polymorphism in Python is the ability of an object to
take many forms. In simple words, polymorphism allows
us to perform the same action in many different ways.

Polymorphism in Built-in
function len()
The built-in function len() calculates the length of
an object depending upon its type. If an object is
a string, it returns the count of characters, and If
an object is a list, it returns the count of items in
a list.
The len() method treats an object as per its class
type.
Polymorphism With Inheritance

• Using method overriding polymorphism allows us to


defines methods in the child class that have the same
name as the methods in the parent class. This process
of re-implementing the inherited method in the
child class is known as Method Overriding.

• Advantage of method overriding


• It is effective when we want to extend the functionality by
altering the inherited method.
• Method overriding is useful when a parent class has multiple
child classes, and one of that child class wants to redefine the
method.
Example: Method Overriding
Polymorphism In Class
methods

Python allows different classes to have methods


with the same name.
• Let’s design a different class in the same
way by adding the same methods in two or
more classes.
• Next, create an object of each class
• Next, add all objects in a tuple.
• In the end, iterate the tuple using a for loop
and call methods of a object without
checking its class
Polymorphism with
Function and Objects

• We can create polymorphism


with a function that can take
any object as a parameter
and execute its method
without checking its class
type.
• Example

You might also like