A 07 OOPs Concepts With Python
A 07 OOPs Concepts With Python
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
Encapsulation Inheritance
Polymorphism Abstraction
Objects and Classes
Discussion
Python
An object represents an entity in the real world that can be distinctly identified. An object
consists of the following components:
Object: Dog
Age Sleep
Color Eat
Classes
Classes
02
Example
class Dog: Here, the class keyword
pass is used to define an
empty class Dog.
Methods are functions defined inside a class. They are invoked by objects to perform actions
on other objects.
object.
__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.
The self keyword in the class method definitions represents the object. It helps
to access the attributes and methods.
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.
Unlike other programming languages (e.g., C++), destructors are not needed in
Python classes.
Attributes
Object: Dog
Identity Attribute
Age
Color
Types of Attributes
A particular instance of a
class owns the variables.
They are variables that are defined inside a class but outside of any method. Class
attributes have the following characteristics:
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.
01 02 03
Access Modifiers: Public Access Modifier
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
Example
Access Modifiers: Protected Access Modifier
01 02
Example
Access Modifiers: Private 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
Example
Assisted Practice: Access Modifiers
Duration: 10 mins
Objective : To demonstrate public protected and private access modifiers.
Tasks to perform:
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
• What is encapsulation?
• What is polymorphism?
Encapsulation
Encapsulation is the process of binding data members and member functions into a single unit.
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.
The son is tall and fair. This indicates that he has inherited the features of his father and mother,
respectively.
Types of Inheritance
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
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
Example
Inheritance: Multiple Inheritance
A class that is derived from more than one parent class is called multiple inheritances.
Child class
Multiple Inheritance: Example
Example
Inheritance: Hierarchical Inheritance
Parent class
Example
Hello I am child 1.
Hello I am child 2.
Assisted Practice: Inheritance
Duration: 5 mins
Problem Scenario: Write a program to demonstrate inheritance using classes, objects, and methods
Tasks to perform:
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.
Polymorphism
Duration: 10 mins
Problem Scenario: Write a program to demonstrate polymorphism using classes,
objects, and methods
Tasks to perform:
2. Create the objects of the base class and call the methods
Abstraction
Abstraction
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
• 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
Tasks to perform:
2. Create a base class containing abstract methods and derived classes containing non-abstract
methods
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.