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

Lesson 04 OOPs Concepts With Python

Uploaded by

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

Lesson 04 OOPs Concepts With Python

Uploaded by

Bharat Behl
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Programming Basics and Data Analytics

with Python
OOPs Concepts with Python
Learning Objectives

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

Explain OOP and its characteristics

Identify objects and classes

Describe methods, attributes, and access modifiers

Define abstraction, encapsulation, inheritance, and


polymorphism with real-life examples
Object-Oriented Programming Language
What Is OOPs?

OOPs refers to languages that use objects in programming. Object-oriented programming aims to
implement real-world entities such as inheritance, hiding, and polymorphism in programming.

Characteristics of OOPs

• OOPs uses a bottom-up approach.


• The program is divided into objects.
• OOPs uses access modifiers.
• OOPs is more secure than procedural languages.
• Objects can move freely within member functions.
OOPS Concepts

Abstraction Encapsulation

Inheritance Polymorphism
Objects and Classes
Objects

Object-oriented programming (OOP) involves programming using objects. An object represents an


entity in the real-world that can be distinctly identified.

An object consists of identity, state, and behavior.

• Identity: It gives a unique name to an object.


• State: It reflects the properties of an object.
• Behavior: It reflects the response of an object with other objects.

Example of an object: Dog


Identity State/Attribute Behavior
Name of the dog Breed Bark
Age Sleep
Color Eat
Classes

A class is a blueprint for an object. Objects with the same properties and methods are defined inside
the class.

class Dog:
pass

Here, we use the keyword class to define an empty class Dog.

Note

We construct instances from a class. An instance is a specific object created from a particular class.
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


class Person:
__init__ is a method that is
# init method or constructor
automatically called when
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.
Attributes

Attributes are defined within a class and outside any method. Attributes define the characteristics of
any object.

Identity Attributes
Name of dog Breed
Age
Color
Access Modifiers
Access Modifiers

A class in Python has three types of access modifiers.

Public Access Modifiers

Protected Access
Modifiers

Private Access Modifiers


Access Modifiers

Members of a class that are declared public are easily


accessible from any part of the program. All data
members and member functions of a class are public
Public Access Modifiers by default.

Protected Access
Modifiers
Example:
class Dog:
Private Access Modifiers
# constructor
def __init__(self, name, age):

# public access modifiers


self.dogName = name
self.dogage = age
Access Modifiers

Members of a class that are declared protected are


only accessible to a class derived from it. Data
members of a class are declared protected by adding
Public Access Modifiers a single underscore symbol (_) before the data
member of that class.

Protected Access
Modifiers
Example:

class Dog:
Private Access Modifiers # protected access modifiers
_name = None
_age = None
_breed = None
Access Modifiers

Members of a class that are declared private are


accessible within the class only. Private access
modifier is the most secure access modifier. Data
Public Access Modifiers members of a class are declared private by adding a
double underscore symbol (__) before the data
member of that class.
Protected Access
Modifiers
Example:

class Dog:
Private Access Modifiers # private access modifiers
__name = None
__age = None
__breed = None
Objects and Classes

Problem Statement: Write a program to demonstrate objects and classes using methods and attributes.

Steps to perform:
1. Create a class
2. Declare the attributes
3. Make a method
4. Initiate the objects
5. Access class attributes and method through objects
Abstraction
Abstraction

Abstraction is the property by virtue of which only the essential details are displayed to the user.

Example: When you press a key on the keyboard, the relevant character appears on the screen. It is
not essential for you to know how exactly this works. This is called abstraction.
Abstraction

Problem Statement: Write a program to demonstrate abstraction using classes, objects, and methods.

Steps to Perform:
1. Create a class called Bill
2. Define a function to initialize variables
3. Create a class called Debit Card Payment
4. Create an object from the class Debit Card Payment
5. Create an object from the class Bill
6. Check whether the object is an instance of class Bill or not
Encapsulation
Encapsulation

Encapsulation is a process of binding data members and member functions into a single unit.
Encapsulation hides the state of a structured data object inside a class, preventing unauthorized access
to an unauthorized person.

Example:

class Encapsulation:
Example: Only the chemist in a drug store has
def __init__(self, a, b,c):
access to medicines. This reduces the risk of
self.public = a
unauthorized people taking any unintended
self._protected = b
medicines.
self.__private = c
Encapsulation

Problem Statement: Write a program to demonstrate encapsulation using classes, objects, and methods.

Steps to Perform:
1. Create a class called Employee
2. Declare variables in the initiation function
3. Make a method to print the variables
4. Create an object for the class Employee
5. Call the display method
Inheritance
Inheritance

Inheritance is the process of forming a new class from an existing class or a base class. The base class
is also known as parent class or super class. The new class that is formed is called derived class.

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

Father (Base class) Mother (Base class)


Tall Short
Dark Fair

Son (Derived class)


Tall
Fair

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

Single inheritance: Multilevel inheritance:


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

Multiple inheritance: Hierarchical inheritance:


A class can inherit from More than one sub class is
more than one class. inherited from a single base
class.
Inheritance

Problem Statement: Write a program to demonstrate inheritance using classes, objects, and methods.

Steps to Perform:
1. Create a base class
2. Create a derived class
3. Call the constructor of the base class
4. Multiply the variable of the base class and the derived class
Polymorphism
Polymorphism

Polymorphism is a Greek word that means many shaped. Polymorphism is the ability of a message to
be displayed in more than one form.

Example: A woman can be a mother, a wife, a daughter, a teacher, and an employee at the same time.
Types of Polymorphism

Polymorphism

Compile time Runtime

Method overloading Operator overriding Virtual functions


Polymorphism

Problem Statement: Write a program to demonstrate polymorphism using classes, objects, and methods.

Steps to Perform:
1. Create a class called Ohio
2. Make a method to print a statement
3. Create a class called California
4. Make a method to print a statement
5. Make two objects of the two classes having the same method
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 is 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.
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.

You might also like