KEMBAR78
Object Oriented Programming by Using Python | PDF
Object-Oriented Programming in Python
Classes, Objects, Inheritance, Polymorphism More
Madhusudhana Rao Baswani
Python Training Series
July 23, 2025
Agenda
1 OOP Basics
2 Core Terminology
3 Classes & Objects
4 Constructors & Methods
5 Encapsulation
6 Inheritance
7 Polymorphism
8 Abstraction
Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 2 / 17
What is OOP?
Programming style that bundles data & operations
into objects.
Promotes modular, reusable, and maintainable code.
Key pillars: Encapsulation, Inheritance, Polymorphism, and Abstraction.
Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 3 / 17
Why OOP in Python?
Everything in Python is an object — even functions and types.
Easy syntax for defining classes and methods.
Supports dynamic typing and duck typing for flexible design.
Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 4 / 17
Glossary
Class Blueprint for creating objects.
Object Instance of a class containing state & behavior.
Data Member Variable defined in a class (instance or static).
Method Function defined inside a class.
Instance/Static & Local Vars Object-level, class-level, and method-scope variables.
Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 5 / 17
Defining a Class
class Student:
""" Represents a student entity """
school = "Leela High" # static var
def __init__(self , name , marks):
self.name = name # instance vars
self.marks = marks
Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 6 / 17
Instantiating Objects
s1 = Student("Kajal", 90)
s2 = Student("Rohith", 85)
print(s1.name , s1.marks)
Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 7 / 17
Constructor
i nit
Automatically invoked during object creation.
First parameter self points to the current object.
Used to initialize instance variables.
Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 8 / 17
Method Types
Type First Param
Instance Method self (object)
Class Method cls (class)
Static Method none
Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 9 / 17
Example: Class & Static Methods
class MathDemo:
factor = 2 # static variable
@classmethod
def double(cls , x):
return x * cls.factor # uses class var
@staticmethod
def add(x, y):
return x + y
Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 10 / 17
Private Attributes
class Car:
def __init__(self):
self.__maxspeed = 200 # private
def drive(self):
print(self.__maxspeed)
Access via name-mangling: obj._Car__maxspeed
Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 11 / 17
Single Inheritance
class Animal:
def eat(self):
print(’Eating ’)
class Dog(Animal):
def bark(self):
print(’Barking ’)
d = Dog(); d.eat(); d.bark()
Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 12 / 17
Types of Inheritance
Single
Multiple
Multilevel
Hierarchical
Hybrid
Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 13 / 17
Compile-time vs Runtime
Operator Overloading: + works for numbers and strings.
Method Overloading: Not natively supported; emulate with default/args.
Method Overriding: Redefine a parent method in child class.
Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 14 / 17
Operator Overloading Example
class Book:
def __init__(self , pages):
self.pages = pages
def __add__(self , other):
return self.pages + other.pages
print(Book (100) + Book (200)) # 300
Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 15 / 17
Abstract Base Class
from abc import ABC , abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 16 / 17
Questions?
Thank You!
Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 17 / 17

Object Oriented Programming by Using Python

  • 1.
    Object-Oriented Programming inPython Classes, Objects, Inheritance, Polymorphism More Madhusudhana Rao Baswani Python Training Series July 23, 2025
  • 2.
    Agenda 1 OOP Basics 2Core Terminology 3 Classes & Objects 4 Constructors & Methods 5 Encapsulation 6 Inheritance 7 Polymorphism 8 Abstraction Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 2 / 17
  • 3.
    What is OOP? Programmingstyle that bundles data & operations into objects. Promotes modular, reusable, and maintainable code. Key pillars: Encapsulation, Inheritance, Polymorphism, and Abstraction. Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 3 / 17
  • 4.
    Why OOP inPython? Everything in Python is an object — even functions and types. Easy syntax for defining classes and methods. Supports dynamic typing and duck typing for flexible design. Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 4 / 17
  • 5.
    Glossary Class Blueprint forcreating objects. Object Instance of a class containing state & behavior. Data Member Variable defined in a class (instance or static). Method Function defined inside a class. Instance/Static & Local Vars Object-level, class-level, and method-scope variables. Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 5 / 17
  • 6.
    Defining a Class classStudent: """ Represents a student entity """ school = "Leela High" # static var def __init__(self , name , marks): self.name = name # instance vars self.marks = marks Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 6 / 17
  • 7.
    Instantiating Objects s1 =Student("Kajal", 90) s2 = Student("Rohith", 85) print(s1.name , s1.marks) Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 7 / 17
  • 8.
    Constructor i nit Automatically invokedduring object creation. First parameter self points to the current object. Used to initialize instance variables. Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 8 / 17
  • 9.
    Method Types Type FirstParam Instance Method self (object) Class Method cls (class) Static Method none Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 9 / 17
  • 10.
    Example: Class &Static Methods class MathDemo: factor = 2 # static variable @classmethod def double(cls , x): return x * cls.factor # uses class var @staticmethod def add(x, y): return x + y Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 10 / 17
  • 11.
    Private Attributes class Car: def__init__(self): self.__maxspeed = 200 # private def drive(self): print(self.__maxspeed) Access via name-mangling: obj._Car__maxspeed Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 11 / 17
  • 12.
    Single Inheritance class Animal: defeat(self): print(’Eating ’) class Dog(Animal): def bark(self): print(’Barking ’) d = Dog(); d.eat(); d.bark() Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 12 / 17
  • 13.
    Types of Inheritance Single Multiple Multilevel Hierarchical Hybrid MadhusudhanaRao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 13 / 17
  • 14.
    Compile-time vs Runtime OperatorOverloading: + works for numbers and strings. Method Overloading: Not natively supported; emulate with default/args. Method Overriding: Redefine a parent method in child class. Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 14 / 17
  • 15.
    Operator Overloading Example classBook: def __init__(self , pages): self.pages = pages def __add__(self , other): return self.pages + other.pages print(Book (100) + Book (200)) # 300 Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 15 / 17
  • 16.
    Abstract Base Class fromabc import ABC , abstractmethod class Shape(ABC): @abstractmethod def area(self): pass Madhusudhana Rao Baswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 16 / 17
  • 17.
    Questions? Thank You! Madhusudhana RaoBaswani (Python Training Series) Object-Oriented Programming in Python July 23, 2025 17 / 17