Object-Oriented Programming in Python.pptx

shilpamathur13 3 views 29 slides Oct 30, 2025
Slide 1
Slide 1 of 29
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29

About This Presentation

In Python, classes and objects define reusable blueprints for creating objects with attributes and behaviors, using concepts like constructors, self, and inner classes. OOP principles such as polymorphism (method overloading/overriding, abstract classes), encapsulation (private/protected members), i...


Slide Content

Module 3 Object Oriented Programming

OOPs Concepts: Class and Objects Inheritance Polymorphism Encapsulation What is a Class and Objects in Python? 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. Every object has the following property: Identity: Every object must be uniquely identified. State: An object has an attribute that represents a state of an object, and it also reflects the property of an object. Behavior: An object has methods that represent its behavior.

Create a Class in Python class class_name: <statement 1> <statement 2> . . <statement N> Example: class Student: name = "Shilpa" s1 = Student() print(s1.name) Output: Shilpa

Class Variables /Static Variables: ● Shared among all instances of the class. ● Defined directly in the class body. ● Changing the value of a class attribute affects all instances unless they have an instance attribute with the same name.

Instance Variables : ● Unique to each instance of the class. ● Defined within the constructor method __init__() or assigned explicitly to the instance. ● Changing the value of an instance attribute only affects that particular instance

Self-Variables, Constructors, Inner Classes ‘self’ is a default variable that contains the memory address of the instance of the current class. For example, we create an instance to Student class as: s1 = Student() Here, ‘s1’ contains the memory address of the instance. This memory address is internally and by default passed to ‘self’ variable. Since ‘self’ knows the memory address of the instance, it can refer to all the members of the instance. We use ‘self’ in two ways: The ‘self’ variable is used as first parameter in the constructor as: def __init__(self): In this case, ‘self’ can be used to refer to the instance variables inside the constructor. ‘self’ can be used as first parameter in the instance methods as: def talk(self): Here, talk() is instance method as it acts on the instance variables.

Constructor A constructor is a special method that is used to initialize the instance variables of a class. In the constructor, we create the instance variables and initialize them with some starting values. The first parameter of the constructor will be ‘self’ variable that contains the memory address of the instance. For example, def __init__(self): self.name = ‘Vishnu’ self.marks = 900 Here, the constructor has only one parameter, i.e. ‘self’. Using ‘self.name’ and ‘self.marks’, we can access the instance variables of the class. A constructor is called at the time of creating an instance. So, the above constructor will be called when we create an instance as: s1 = Student() def __init__(self, n = ‘’, m=0): self.name = n self.marks = m s1 = Student(‘Lakshmi Roy’, 880)

Inner Class I f we write class B inside class A, then B is called inner class or nested class. Inner classes are useful when we want to sub group the data of a class. For example, let’s take a person’s data like name, age, date of birth etc. Here, name contains a single value like ‘Charles’, age contains a single value like ‘30’ but the date of birth does not contain a single value. Rather, it contains three values like date, month and year. So, we need to take these three values as a sub group.

Inheritance in Python: Inheritance in Python is a mechanism that allows one class (child class) to inherit attributes and methods from another class (parent class). It enables code reusability and establishes relationships between classes, such as "is-a" relationships. Key Definition 1. Single Inheritance: When a class inherits from a single parent class. 2. Multilevel Inheritance: When a class is derived from a class that is also derived from another class. 3. Multiple Inheritance: When a class inherits from more than one parent class. 4. Using super() Method: The super() method is used to call methods from the parent class in a child class, often in cases of constructor inheritance.

Single Inheritance # Base class class Parent: def func1(self): print("This function is in parent class.") # Derived class class Child(Parent): def func2(self): print("This function is in child class.") obj = Child() obj.func1() obj.func2()

Multiple Inheritance

The super() Method

MRO If the sub class has a constructor, it overrides the super class constructor and hence the super class constructor is not available to the sub class. But writing constructor is very common to initialize the instance variables. In multiple inheritance, let’s assume that a sub class ‘C’ is derived from two super classes ‘A’ and ‘B’ having their own constructors. Even the sub class ‘C’ also has its constructor. To derive C from A and B, we write: class C(A, B):

Polymorphism Method Overloading

Method Overriding

Encapsulation Private and Protected Members

Encapsulation is one of the key principles of Object-Oriented Programming (OOP). It involves bundling data (attributes) and methods (functions) that operate on the data into a single unit, called a class, while restricting access to some components using access modifiers like private and protected.

● Private Members: In Python, private members are prefixed with double underscores (__). This prevents direct access to the variable outside of its class, thanks to name mangling. ● Protected Members: These are prefixed with a single underscore (_), indicating they are intended for internal use within the class and its subclasses but can still be accessed from outside the class if needed.

Private Members: Private members are intended to be accessible only within the class. In Python, private members are defined by prefixing the member name with double underscores (__). Python performs name mangling on private members, which means the member is not directly accessible outside the class. (Python changes the name of private members internally to _ClassName__memberName . This is known as name mangling.)

Protected Members: Protected members are intended to be accessible within the class and its subclasses. In Python, protected members are indicated by a single underscore prefix (_). This is a convention and does not enforce strict access control; it’s a way of indicating that the member should not be accessed directly outside the class hierarchy.

1. Which of the following is true about private members in Python? a) They can be accessed directly from outside the class. b) They are prefixed with a single underscore. c) They are prefixed with double underscores and subject to name mangling. d) There is no such concept in Python. 2. What happens when you try to access a private member outside its class? a) It throws a SyntaxError. b) It throws an AttributeError. c) It accesses the private member. d) It automatically converts it to a protected member

3. Which of the following is a convention for indicating a protected member in Python? a) Prefix with double underscores. b) No special prefix needed. c) Prefix with a single underscore. d) Prefix with a tilde (~) 4. Which statement is true about protected members in Python? a) They can only be accessed within the same class. b) They can be accessed from outside the class but should not be. c) They are accessible by all instances. d) They cannot be inherited.

5. How does Python handle private members internally? a) By making them public after runtime. b) By renaming them using name mangling (_ClassName__MemberName). c) By making them inaccessible forever. d) By converting them into protected members.

Exercise 1. Write a Python program to demonstrate the use of private members and how to access them using a method within the class. 2. Modify the program to demonstrate how protected members work and how they can be accessed from a subclass. 3. Explain the concept of encapsulation in Python with a real-world analogy. 4. Write a program with a class that has both private and protected members. Show how to use getter methods for private members and directly access protected members. 5. Why is it not recommended to access protected members from outside the class? Justify your answer with an example.

Abstract Class and Interfaces

Interface O nly abstract methods and there are no concrete methods.