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

A 07 OOPs Concepts With Python

Uploaded by

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

A 07 OOPs Concepts With Python

Uploaded by

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

Python Basics

OOPs Concepts with Python


Learning Objectives

By the end of this lesson, you will be able to:

Explain the concept of object-oriented programming (OOP) and its characteristics

Identify objects and classes

Describe methods, attributes, and access modifiers

Define abstraction, encapsulation, inheritance, and polymorphism


with real-life examples
Business Scenario

ABC is a banking firm that is currently developing a banking management system


application. This application should include customer information accessible to
bank employees. Employees should be able to access and edit this information in
response to customer requests. However, the current application lacks security,
as workers at all levels can access a large amount of data. Additionally, the bank
wants to customize the application for specific branches.
The firm has decided to update the application, ensuring that only the necessary
customer details are available to employees. Critical information will only be
accessible to senior officials. To accomplish this, the organization will apply
object-oriented programming (OOP) concepts, including encapsulation and
abstraction. Methods, attributes, and access modifiers will also be utilized in the
update.
Object-Oriented Programming Language
What Is OOPs?

OOPs refer to languages that use objects in programming. It aims to implement real-world entities,
such as inheritance, information hiding, and polymorphism in programming.
OOP: Concepts

The four concepts of object-oriented programming are:

Encapsulation Inheritance

Polymorphism Abstraction
Objects and Classes
Discussion
Python

Are objects and classes the same?

• What are objects and their components?


• What are classes?
Objects

An object represents an entity in the real world that can be distinctly identified. An object
consists of the following components:

Identity: It gives a unique


01 name to an object.

Components of an State: It reflects the


02 properties of an object.
object

Behavior: It reflects the


03
response of an object to
other objects.
Objects: Example

An example of an object is given below:

Object: Dog

Identity State or Attribute Behavior

Name of the dog Breed Bark

Age Sleep

Color Eat
Classes

A class is a blueprint for an object.

Classes

The keyword class is used A class defines objects that


to create a class. 01 03 share the same properties
and methods.

02

A class is like an object constructor for creating objects.


Classes: Example

An example of a class is given below:

Example
class Dog: Here, the class keyword
pass is used to define an
empty class Dog.

An instance is a specific object created from a particular class.


Python

Are objects and classes the same?

• What are objects and their components?


Answer: An object represents an entity in the real world that can be distinctly
identified. An object consists of its identity, state, and behavior.
• What are classes?
Answer: A class is a blueprint for an object. A class defines objects that share
the same properties and methods.
Methods and Attributes
Methods

Methods are functions defined inside a class. They are invoked by objects to perform actions
on other objects.

# A sample class with init method


__init__ is a method that is class Person:
automatically called when # init method or constructor
def __init__(self, name):
memory is allocated to a new ])
self.name = name

object.

• In the init method, self refers to the newly created object.


• In other class methods, it refers to the instance whose
method was called.
The __init__ method

The __init__ method in Python is equivalent to the constructor in other


languages.

This method is invoked every time an object of that class is created.

__init__
method
An __init__ method can accept as many parameters as necessary to
initialize the object's attributes. It is used only within the class.

In Python, default arguments can be used instead of passing values to


constructors.
Self

The self keyword in the class method definitions represents the object. It helps
to access the attributes and methods.

It binds the attributes with the given arguments.

self
keyword
self refers to the object being created, or the instance whose method was
called, in the __init__ method.

It is the first parameter provided to methods that refer to the instance on which
the method was called.
Instantiating Objects

It refers to the creation of objects or instances of a given class. To instantiate a class, the user needs to
call the class as if it is a function, passing the arguments as defined in the __init__ function of the class.

Example: Create an object for student class

• Here ‘st1’ is an object of a class student.


• The values passed in a class are the arguments
specified in the __init__ function of the class.
Deleting Instances

In Python, it is not necessary to explicitly delete an object after use.

It automatically recognizes and releases the memory when all references to a


particular block of memory are no longer needed.

Python has automatic garbage collection.

Unlike other programming languages (e.g., C++), destructors are not needed in
Python classes.
Attributes

The non-method data stored by the objects are called attributes.

Object: Dog

Identity Attribute

Name of the dog Breed

Age

Color
Types of Attributes

A Python object consists of two types of attributes:

Data attributes Class attributes


Data Attributes

The following are some characteristics of data attributes:

A particular instance of a
class owns the variables.

Each instance has its The __init__() method


value for it. creates and
initializes variables.

Data attributes are


referred inside the class
using self keyword.
Class Attributes

They are variables that are defined inside a class but outside of any method. Class
attributes have the following characteristics:

They can be accessed


They are shared by all using the class name
instances of the class. or an instance of the
class.

They can be used to


They can be accessed store constants,
from outside the class, default values, or any
which can lead to other data that needs
unexpected behavior. to be shared by all
instances of the class.
Assisted Practice: Create a Class with Attributes And
Methods

Duration: 5 mins
Problem Scenario: Write a program to demonstrate objects and classes using methods and attributes

Objective: In this demonstration, you will learn how to create a class and define methods and
attributes for it.

Tasks to Perform:

1. Create a class
2. Declare the desired attributes
3. Create a method that displays the information
4. Initiate the objects
5. Access class attributes and methods through objects
Access Modifiers
Access Modifiers

A class in Python has three types of access modifiers. They are special keywords that
allow for changing the behavior or properties of class attributes and methods.

Public access Protected access Private access


modifiers modifiers modifiers

01 02 03
Access Modifiers: Public Access Modifier

Public access modifiers have two characteristics:

01 02

Data members of a
class that are All data members and
declared public can member functions of a
be accessed from class are public by
any part of the default.
program.
Public Access Modifier: Example

The following example explains the public access modifier:

Example
Access Modifiers: Protected Access Modifier

Protected access modifiers have two characteristics:

01 02

Data members of a class


Members of a class
are declared protected
that are declared
protected are only by adding a single
accessible to a class underscore symbol (_)
derived from it. before the data member
of that class.
Protected Access Modifier: Example

The following example explains the protected access modifier:

Example
Access Modifiers: Private Access Modifier

A private access modifier is the most secure access modifier.

01 02

Data members of a
Private members class are declared
of a class can be private by adding a
accessed within double underscore
the class only. symbol ( _ _ ) before
the data member
name.
Private Access Modifier: Example

The following example explains the private access modifier:

Example
Assisted Practice: Access Modifiers

Duration: 10 mins
Objective : To demonstrate public protected and private access modifiers.

Tasks to perform:

1. Create a parent class with public, private and protected members

2. Create a child class and invoke the public, private and protected members of the parent class

3. Create an object of the child class and call the method to display the data
Encapsulation
Discussion
Encapsulation and Polymorphism

You are working as a data scientist in an application development project


where one phase of development is already completed. Once you analyze
the project code, you determine that the code could have been written
better. Some modules could have been displayed in more than one form
instead of coding them separately.

To understand this better, it is important to know about OOPs in detail.

• What is encapsulation?

• What is polymorphism?
Encapsulation

Encapsulation is the process of binding data members and member functions into a single unit.

It hides the state of a It acts as a protective


structured data object barrier that restricts direct
inside a class, preventing Features access to variables and
unauthorized access to an methods. It is also known
unauthorized person. as data hiding.
Encapsulation: Example

At a medical store, only the chemist has access to the medicines based on the prescription.
This reduces the risk of taking any medicine that is not intended for a patient.

Example
Inheritance
Inheritance

Inheritance is the process of forming a new class from an existing class or a base class.

Example: A family has three members, father, mother, and son.

Father (Base class) Mother (Base class)


Tall Short Also known
Dark Fair as super
class

Son (Derived class)


Also known
Tall as sub class
Fair

The son is tall and fair. This indicates that he has inherited the features of his father and mother,
respectively.
Types of Inheritance

There are four types of inheritance:

Single level inheritance:


Multilevel inheritance:
A class can inherit from only
A derived class is created
one class.
from another derived class.

Hierarchical inheritance:
Multiple inheritance:
A base class can have
A class can inherit from multiple subclasses
more than one class. inherited from it.
Inheritance: Single Level Inheritance

A class that is derived from one parent class is called single-level inheritance.

Parent class

Child class
Single Level Inheritance: Example

The following is an example of single level inheritance:

Example
Inheritance: Multilevel Inheritance

In multilevel inheritance, the features of the parent class and the child class
are further inherited into the new child class.

Parent class

Child1 Class
Child1 class

Child2 class
Multilevel Inheritance: Example

An example of multilevel inheritance is shown below:

Example
Inheritance: Multiple Inheritance

A class that is derived from more than one parent class is called multiple inheritances.

Parent1 class Parent2 class

Child class
Multiple Inheritance: Example

An example of multiple inheritance is given below:

Example
Inheritance: Hierarchical Inheritance

Hierarchical inheritance is the process of creating multiple derived classes


from a single base class.

Parent class

Child1 class Child2 class


Hierarchical Inheritance: Example

The following example explains hierarchical inheritance:

Example

Hello I am the parent.

Hello I am child 1.

Hello I am the parent.

Hello I am child 2.
Assisted Practice: Inheritance

Duration: 5 mins
Problem Scenario: Write a program to demonstrate inheritance using classes, objects, and methods

Objective: In this demonstration, we will learn how to work with inheritance.

Tasks to perform:

1. Create 2 base classes

2. Create a derived class that derives the attributes of the parent class

3. Create the objects of the derived class and retrieve the attributes of the parent class
Polymorphism
Polymorphism

Mother
• Polymorphism is a Greek word that means
many shapes.

Friend Wife • The ability of a message to be displayed in


more than one form is known as
polymorphism.
• Example: A woman can be a mother, a wife,
a teacher, a sister, and a friend at the same
Woman time.
Sister Teacher
Types of Polymorphism

The types of polymorphism are mentioned below:

Polymorphism

Compile time Runtime

Method overloading Operator Overloading Method Overriding


Assisted Practice: Polymorphism

Duration: 10 mins
Problem Scenario: Write a program to demonstrate polymorphism using classes,
objects, and methods

Objective: In this demonstration, we will learn how to perform polymorphism.

Tasks to perform:

1. Create two classes that contain the same method names

2. Create the objects of the base class and call the methods
Abstraction
Abstraction

It allows the representation of complex systems or ideas in a simplified manner.

Example: When one presses a key on the keyboard, the relevant character appears on the screen. One
doesn't have to know how exactly this works. This is called abstraction.
Abstraction

In Python, abstraction works by incorporating abstract classes and methods.

Abstract methods do not


Abstract Class is a class have implementation in
specified in the codes the abstract class. All
containing abstract 01 implementation is done
methods. inside the sub classes
Abstraction

In Python, abstraction works by incorporating abstract classes and methods.

Only an object of the


An abstract class can only derived class can be used
be inherited. 01 to access the features of
the abstract class.
Encapsulation and Polymorphism

You are working as a data scientist in an application development project where


one phase of development is already completed. Once you analyze the project
code, you determine that the code could have been written better. Some
modules could have been displayed in more than one form instead of coding
them separately.

To understand this better, it is important to know about OOPs in detail.


• What is encapsulation?
Answer: Encapsulation is the process of binding data members and member
functions into a single unit.

• What is polymorphism?
Answer: The ability of a message to be displayed in more than one form is known
as polymorphism.
Assisted Practice: Abstraction

Duration: 5 mins
Problem Scenario: Write a program to demonstrate abstraction in Python

Objective: In this demonstration, we will learn how to implement abstraction.

Tasks to perform:

1. Import the necessary packages for creating an abstract class

2. Create a base class containing abstract methods and derived classes containing non-abstract
methods

3. Implement the methods of abstract class using objects


Key Takeaways

Object-oriented programming aims to implement real-world entities such


as inheritance, hiding, and polymorphism in programming.

An object is an instance of a class.

A class is a blueprint for an object. A class is a definition of objects


with the same properties and methods.

A class in Python has three types of access modifiers: public,


protected, and private.
Knowledge Check
Knowledge
Check
An object is an instance of a(n) ___________________.
1

A. Method

B. Attribute

C. Class

D. Function
Knowledge
Check
An object is an instance of a(n) ___________________.
1

A. Method

B. Attribute

C. Class

D. Function

The correct answer is C

An object is an instance of a class.


Knowledge
Check
Which of the following is NOT an OOPs concept?
2

A. Inheritance

B. Compilation

C. Abstraction

D. Encapsulation
Knowledge
Check
Which of the following is NOT an OOPs concept?
2

A. Inheritance

B. Compilation

C. Abstraction

D. Encapsulation

The correct answer are B

There are four OOPS concepts: Inheritance, Encapsulation, Polymorphism, and Abstraction.
Knowledge
Check
Which of the following is a type of polymorphism?
3

A. Compile time polymorphism

B. Runtime polymorphism

C. Multiple polymorphism

D. Multilevel polymorphism
Knowledge
Check
Which of the following is a type of polymorphism? (Select all that apply)
3

A. Compile time polymorphism

B. Runtime polymorphism

C. Multiple polymorphism

D. Multilevel polymorphism

The correct answer is A and B

The types of polymorphism are compile time polymorphism and runtime polymorphism.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy