Lesson 04 OOPs Concepts With Python
Lesson 04 OOPs Concepts With Python
with Python
OOPs Concepts with Python
Learning Objectives
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
Abstraction Encapsulation
Inheritance Polymorphism
Objects and Classes
Objects
A class is a blueprint for an object. Objects with the same properties and methods are defined inside
the class.
class Dog:
pass
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.
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
Protected Access
Modifiers
Protected Access
Modifiers
Example:
class Dog:
Private Access Modifiers
# constructor
def __init__(self, name, age):
Protected Access
Modifiers
Example:
class Dog:
Private Access Modifiers # protected access modifiers
_name = None
_age = None
_breed = None
Access Modifiers
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.
The son is tall and fair. This indicates that he has inherited the features of his father and mother,
respectively.
Types of 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
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
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
There are four OOPS concepts: Inheritance, Encapsulation, Polymorphism, and Abstraction.
Knowledge
Check
Which of the following is a type of polymorphism?
3
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
b. Runtime polymorphism
c. Multiple polymorphism
d. Multilevel polymorphism
The types of polymorphism are compile time polymorphism and runtime polymorphism.
Key Takeaways